Lily
Lily

Reputation: 707

Add repeated item IDs on consecutive indices Java List

I am working on Java 11. I have a scenario that if the list.size()<14 then I have to repeat the values on the consecutive index and the method should return the list.size()=14. I have tried the following code which is giving me results but not 100% as per the requirements.

private void addRepeatedItemIds() {
        List<String> newIds = new ArrayList<>();
        List<String> originalIds = Arrays.asList("1", "2", "3", "4", "5");
        if (originalIds.size() >= 7) {
            System.out.println("Size is good we could add the items at consecutive index.");
            for (int i = 0; i < originalIds.size(); i++) {
                newIds.add(originalIds.get(i));
                newIds.add(originalIds.get(i));
            }
        } else {
            System.out.println("Size is below average.");
            while (newIds.size() < 14) {
                for (int i = 0; i < originalIds.size(); i++) {
                    newIds.add(originalIds.get(i));
                    newIds.add(originalIds.get(i));

                }
            }

        }

        System.out.println(newIds);
        System.out.println(newIds.size());

    }

I have tried two different scenarios. Whose results are given below?

  1. When given Ids size is greater than 7. Test Case1
  2. When given Ids size is less than 7 test case 2

Expected Results

Test case 1 : [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8]

Test case 2 : [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 1, 2, 2]

Meanwhile the newIds.size() should be 14 and no items from the orignalIds list should be skipped.

Upvotes: 0

Views: 127

Answers (4)

Rishabh Awatani
Rishabh Awatani

Reputation: 99

I think this can work

List<String> newIds = new ArrayList<>();
    List<String> originalIds = Arrays.asList("1", "2", "3", "4", "5","6","7","8");
    if (originalIds.size() >= 7) {
        System.out.println("Size is good we could add the items at consecutive index.");

            for (int i = 0; i < originalIds.size(); i++) {
                newIds.add(originalIds.get(i));
                newIds.add(originalIds.get(i));
            }

            int j = newIds.size() - 14;
            while (j>0){
                newIds.remove(newIds.size() - (j * 2));
                j--;
            }
    } else {
        System.out.println("Size is below average.");
        while (newIds.size() < 14) {
                for (int i = 0; i < originalIds.size(); i++) {
                    newIds.add(originalIds.get(i));
                    newIds.add(originalIds.get(i));

                    if(newIds.size() == 14){
                        break;
                    }
                }
        }

    }

    System.out.println(newIds);
    System.out.println(newIds.size());

Upvotes: 1

Eritrean
Eritrean

Reputation: 16498

Something like below should work if I understand your goal correctly:

private void addRepeatedItemIds() {
    List<String> originalIds = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9");
    List<String> newIds = new ArrayList<>();

    if (originalIds.size() >= 14) {
        newIds.addAll(originalIds.subList(0, 14));
    } 

    else if (originalIds.size() >= 7) {
        for (int i = 0; i < originalIds.size() - (originalIds.size() * 2 - 14); i++) {
            newIds.add(originalIds.get(i));
            newIds.add(originalIds.get(i));
        }
        newIds.addAll(originalIds.subList(originalIds.size() - (originalIds.size() * 2 - 14), originalIds.size()));
    } 

    else if (originalIds.size() >= 1) {
        for (int i = 0; i < 7; i++) {
            newIds.add(originalIds.get(i % originalIds.size()));
            newIds.add(originalIds.get(i % originalIds.size()));
        }
    }

    System.out.println(newIds);
}

Please note that I have modified your if-else blocks to cover all edge cases.

  • If the original list size >= 14 you just need to copy the first 14 items
  • If the size of the original list is between 7 and 14 just calculate how many and which items you need to duplicate originalIds.size() * 2 - 14 (for example if original list contains 10 elements and you duplicate each item you will have 20 items. But since you need 14 items you calculate 20 - 14 = 6, which means the last 6 items will be appended and the first 4 items duplicated.
  • If size < 7 just keep adding two items at a time 7x (till result list have a size of 14)

Upvotes: 1

Yawei Xi
Yawei Xi

Reputation: 11

The while condition will not be judged until the for loop ends.

Upvotes: 0

Related Questions