在数组的每第二个元素之后添加数据。

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

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&lt;Object&gt; list = new ArrayList&lt;&gt;();
list.add(&quot;messi&quot;);
list.add(&quot;ronaldo&quot;);
list.add(&quot;rooney&quot;);
list.add(&quot;pogba&quot;);
list.add(&quot;hazard&quot;);

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&lt;Object&gt; list = new ArrayList&lt;&gt;();
        list.add(&quot;messi&quot;);
        list.add(&quot;ronaldo&quot;);
        list.add(&quot;rooney&quot;);
        list.add(&quot;pogba&quot;);
        list.add(&quot;hazard&quot;);

        int counter = 0 ;
        List&lt;Object&gt; data = new ArrayList&lt;&gt;();
        for(Object obj : list){
            data.add(obj);
            counter = counter + 1;
            if(counter%2 == 0)
                data.add(&quot;DATA&quot;);
        } 
        
        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&lt;Object&gt; list = new ArrayList&lt;&gt;();
List&lt;Object&gt; newlist = new ArrayList&lt;&gt;();

list.add(&quot;messi&quot;);
list.add(&quot;ronaldo&quot;);
list.add(&quot;rooney&quot;);
list.add(&quot;pogba&quot;);
list.add(&quot;hazard&quot;);

for(int i = 0; i &lt; 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&lt;Object&gt; list = new ArrayList&lt;&gt;();
        sample.addList(&quot;messi&quot;, list);
        sample.addList(&quot;ronaldo&quot;, list);
        sample.addList(&quot;rooney&quot;, list);
        sample.addList(&quot;pogba&quot;, list);
        sample.addList(&quot;hazard&quot;, list);
        System.out.println(list);
    }

    private static void addList(String value, List&lt;Object&gt; objects) {
          if (!objects.isEmpty() &amp;&amp; (objects.size() % 3 == 2)) {
            objects.add(&quot;Data&quot;);
        }
        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&lt;Object&gt; list = new ArrayList&lt;&gt;();
list.add(&quot;messi&quot;);
list.add(&quot;ronaldo&quot;);
list.add(&quot;rooney&quot;);
list.add(&quot;pogba&quot;);
list.add(&quot;hazard&quot;);
System.out.println(list);

for (int i = 2; i &lt; list.size(); i += 3) {
	list.add(i, &quot;DATA&quot;);
}
System.out.println(list);

Output

[messi, ronaldo, rooney, pogba, hazard]
[messi, ronaldo, DATA, rooney, pogba, DATA, hazard]

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

发表评论

匿名网友

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

确定