英文:
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()<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?
>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<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)
答案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<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());
答案3
得分: 0
while循环的条件直到for循环结束后才会被判断。
英文:
The while condition will not be judged until the for loop ends.
答案4
得分: 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论