英文:
add data after every 2nd element in an array
问题
我想在每两个元素之后向数组中添加对象/数据...为了更清楚起见,我将使用一个简单的示例
我有一个数组列表:
List<Object> list = new ArrayList<>();
list.add("messi");
list.add("ronaldo");
list.add("rooney");
list.add("pogba");
list.add("hazard");
print(list);
//输出:[messi,ronaldo,rooney,pogba,hazard]
我想要的是:
//[messi,ronaldo,DATA,rooney,pogba,DATA,hazard]
我如何实现这个目标。
在每两个元素之后添加数据到数组中。
英文:
I want to add object/data in an array after every 2nd element ....just for making more clear I am going to use a simple example
I have arrayList of :
List<Object> list = new ArrayList<>();
list.add("messi");
list.add("ronaldo");
list.add("rooney");
list.add("pogba");
list.add("hazard");
print(list);
//output : [messi, ronaldo, rooney, pogba,hazard]
What I want is:
//[messi, ronaldo, DATA, rooney, pogba, DATA, hazard]
how I can achieve this.
Adding data in an array after every 2nd element.
答案1
得分: 3
通过维护一个计数器。
public class Main {
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
list.add("messi");
list.add("ronaldo");
list.add("rooney");
list.add("pogba");
list.add("hazard");
int counter = 0;
List<Object> data = new ArrayList<>();
for (Object obj : list) {
data.add(obj);
counter = counter + 1;
if (counter % 2 == 0)
data.add("DATA");
}
for (Object obj : data)
System.out.println(obj);
}
}
英文:
By maintaining a counter.
public class Main {
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
list.add("messi");
list.add("ronaldo");
list.add("rooney");
list.add("pogba");
list.add("hazard");
int counter = 0 ;
List<Object> data = new ArrayList<>();
for(Object obj : list){
data.add(obj);
counter = counter + 1;
if(counter%2 == 0)
data.add("DATA");
}
for(Object obj : data)
System.out.println(obj);
}
}
答案2
得分: 2
所以,你可以做的是创建一个新的列表来存储带有新对象的旧数据:
List<Object> list = new ArrayList<>();
List<Object> newlist = new ArrayList<>();
list.add("messi");
list.add("ronaldo");
list.add("rooney");
list.add("pogba");
list.add("hazard");
for (int i = 0; i < list.size(); i++) {
if (i % 2 == 0) {
// 在每两个项目后添加DATA
newlist.add(data);
}
newlist.add(list.get(i));
}
请有人进行格式化,因为我是从手机上编写的。
英文:
So, you can do is create a new list to store the old data with new object:
List<Object> list = new ArrayList<>();
List<Object> newlist = new ArrayList<>();
list.add("messi");
list.add("ronaldo");
list.add("rooney");
list.add("pogba");
list.add("hazard");
for(int i = 0; i < list.size(); i++) {
if(i%2==0) {
// Add DATA after 2 items
newlist.add(data);
}
newlist.add(list.get(i));
}
Please someone format since I'm writing from my phone.
答案3
得分: 0
class sample {
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
sample.addList("messi", list);
sample.addList("ronaldo", list);
sample.addList("rooney", list);
sample.addList("pogba", list);
sample.addList("hazard", list);
System.out.println(list);
}
private static void addList(String value, List<Object> objects) {
if (!objects.isEmpty() && (objects.size() % 3 == 2)) {
objects.add("Data");
}
objects.add(value);
}
}
可以创建一个如上所述的实用方法。
每次在添加元素之前,检查一下是否 list.size() % 3 == 2
,如果是,则添加额外的元素。
这会对你有所帮助。
英文:
class sample {
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
sample.addList("messi", list);
sample.addList("ronaldo", list);
sample.addList("rooney", list);
sample.addList("pogba", list);
sample.addList("hazard", list);
System.out.println(list);
}
private static void addList(String value, List<Object> objects) {
if (!objects.isEmpty() && (objects.size() % 3 == 2)) {
objects.add("Data");
}
objects.add(value);
}
}
You can create one utility method as mentioned above.
For every time before adding element check that if list.size() % 3 == 2
then add extra element.
This will help you.
答案4
得分: 0
看一下所需的输出和插入的DATA值的索引:
[messi, ronaldo, DATA, rooney, pogba, DATA, hazard]
0 1 2 3 4 5 6
DATA值放在索引2、5、8、11等处。
这意味着这是一个简单的for
循环,调用add(int index, E element)
:
List<Object> list = new ArrayList<>();
list.add("messi");
list.add("ronaldo");
list.add("rooney");
list.add("pogba");
list.add("hazard");
System.out.println(list);
for (int i = 2; i < list.size(); i += 3) {
list.add(i, "DATA");
}
System.out.println(list);
输出
[messi, ronaldo, rooney, pogba, hazard]
[messi, ronaldo, DATA, rooney, pogba, DATA, hazard]
英文:
Look at the desired output and the indexes of the inserted DATA values:
[messi, ronaldo, DATA, rooney, pogba, DATA, hazard]
0 1 2 3 4 5 6
The DATA values go in index 2, 5, 8, 11, ...
Which means it's a simple for
loop calling add(int index, E element)
:
List<Object> list = new ArrayList<>();
list.add("messi");
list.add("ronaldo");
list.add("rooney");
list.add("pogba");
list.add("hazard");
System.out.println(list);
for (int i = 2; i < list.size(); i += 3) {
list.add(i, "DATA");
}
System.out.println(list);
Output
[messi, ronaldo, rooney, pogba, hazard]
[messi, ronaldo, DATA, rooney, pogba, DATA, hazard]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论