Add repeated item IDs on consecutive indices in Java List

huangapple go评论70阅读模式
英文:

Add repeated item IDs on consecutive indices Java List

问题

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());
}

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 originalIds list should be skipped.

英文:

I am working on Java 11. I have a scenario that if the list.size()&lt;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&lt;String&gt; newIds = new ArrayList&lt;&gt;();
        List&lt;String&gt; originalIds = Arrays.asList(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;);
        if (originalIds.size() &gt;= 7) {
            System.out.println(&quot;Size is good we could add the items at consecutive index.&quot;);
            for (int i = 0; i &lt; originalIds.size(); i++) {
                newIds.add(originalIds.get(i));
                newIds.add(originalIds.get(i));
            }
        } else {
            System.out.println(&quot;Size is below average.&quot;);
            while (newIds.size() &lt; 14) {
                for (int i = 0; i &lt; 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.
    Add repeated item IDs on consecutive indices in Java List
  2. When given Ids size is less than 7
    Add repeated item IDs on consecutive indices in Java List

>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.

答案1

得分: 1

如果我正确理解您的目标,类似以下的代码应该可以工作:

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);
}

请注意,我已经修改了您的 if-else 块,以涵盖所有边界情况。

  • 如果原始列表大小大于等于 14,您只需要复制前 14 个项目。
  • 如果原始列表大小介于 7 和 14 之间,只需计算需要复制哪些项以及多少项 originalIds.size() * 2 - 14(例如,如果原始列表包含 10 个元素,并且您复制每个项目,您将有 20 个项目。但由于您需要 14 个项目,您计算 20 - 14 = 6,这意味着最后 6 个项目将被添加,前 4 个项目将被复制。
  • 如果大小小于 7,只需一次添加两个项目,重复 7 次(直到结果列表的大小为 14)。
英文:

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

private void addRepeatedItemIds() {
    List&lt;String&gt; originalIds = Arrays.asList(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;9&quot;);
    List&lt;String&gt; newIds = new ArrayList&lt;&gt;();

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

    else if (originalIds.size() &gt;= 7) {
        for (int i = 0; i &lt; 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() &gt;= 1) {
        for (int i = 0; i &lt; 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)

答案2

得分: 1

我认为这可以生效

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("尺寸适合,我们可以在连续的索引处添加项目。");

    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("尺寸低于平均水平。");
    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());
英文:

I think this can work

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

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

            int j = newIds.size() - 14;
            while (j&gt;0){
                newIds.remove(newIds.size() - (j * 2));
                j--;
            }
    } else {
        System.out.println(&quot;Size is below average.&quot;);
        while (newIds.size() &lt; 14) {
                for (int i = 0; i &lt; 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());

答案3

得分: 0

while循环的条件直到for循环结束后才会被判断。

英文:

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

答案4

得分: 0

当条件满足时跳出,注意第17-19行

英文:

Jump out when conditions are met, note line 17-19

huangapple
  • 本文由 发表于 2020年8月31日 20:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63670848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定