创建一个在Java中合并两个ArrayList的方法。

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

creating a method that combines two ArrayLists in Java

问题

package practice;

import java.util.ArrayList;

public class practice_1 {

    public static void main(String[] args) {
        /**10/3/2020
         * Appends one array list to another
         */

        ArrayList<Integer> partA = new ArrayList<Integer>();
        partA.add(1);
        partA.add(4);
        partA.add(9);
        partA.add(16);
        ArrayList<Integer> partB = new ArrayList<Integer>();
        partB.add(9);
        partB.add(7);
        partB.add(4);
        partB.add(9);
        partB.add(11);

        System.out.println("A: " + partA);
        System.out.println("B: " + partB);
        ArrayList<Integer> appendedList = append(partA, partB);
        System.out.println("Append: " + appendedList);
    }

    public static ArrayList<Integer> append(ArrayList<Integer> partA, ArrayList<Integer> partB) {
        ArrayList<Integer> append = new ArrayList<Integer>();

        for (int x = 0; x < partA.size(); x++) {
            append.add(partA.get(x));
            append.add(partB.get(x));
        }
        return append;
    }
}
英文:

I'm new to programming in Java and I am trying to write a method that combines the contents of two ArrayLists and then prints them. I wrote the array lists and they will print individually fine but I am unsure how to print the result.

The result should be [1, 4, 9, 16, 9, 7, 4, 9, 11]

package practice;

import java.util.ArrayList;

public class practice_1 {

	public static void main(String[] args) {
		/**10/3/2020
		 * Appends one array list to another
		 */
	
		ArrayList &lt;Integer&gt; partA = new ArrayList &lt;Integer&gt;();
			partA.add(1);
			partA.add(4);
			partA.add(9);
			partA.add(16);
		ArrayList &lt;Integer&gt; partB = new ArrayList &lt;Integer&gt;();
	    	partB.add(9);
	    	partB.add(7);
	    	partB.add(4);
	    	partB.add(9);
	    	partB.add(11);
	   	
	   	System.out.println(&quot;A: &quot; + partA);
        System.out.println(&quot;B: &quot; + partB);
		System.out.println(&quot;Append: &quot; + ???); //How do I print the two ArrayLists together?
	}
	public static ArrayList&lt;Integer&gt; append(ArrayList &lt;Integer&gt; partA, ArrayList &lt;Integer&gt; partB)
	{
	ArrayList&lt;Integer&gt; append = new ArrayList&lt;Integer&gt;();
	

		for(int x = 0; x &lt; 1; x++) 
			{
			append.add(partA.get(x));
			append.add(partB.get(x));
			}
		return append;
		}
	}

答案1

得分: 2

使用addAll()方法

List<Integer> partAB = new ArrayList<Integer>();
partAB.addAll(partA);
partAB.addAll(partB);
System.out.println("Append: " + partAB);

查看上下文:

public static void main(String[] args) throws Exception {
    List<Integer> partA = Arrays.asList(1, 4, 9, 16);
    List<Integer> partB = Arrays.asList(9, 7, 4, 9, 11);

    System.out.println("A: " + partA);
    System.out.println("B: " + partB);

    List<Integer> partAB = new ArrayList<Integer>();
    partAB.addAll(partA);
    partAB.addAll(partB);
    System.out.println("Append: " + partAB);
}

输出结果:

A: [1, 4, 9, 16]
B: [9, 7, 4, 9, 11]
Append: [1, 4, 9, 16, 9, 7, 4, 9, 11]
英文:

Use addAll()

    List &lt;Integer&gt; partAB = new ArrayList &lt;Integer&gt;();
partAB.addAll(partA);
partAB.addAll(partB);
System.out.println(&quot;Append: &quot; + partAB);

See context:

public static void main(String[] args) throws Exception {
List &lt;Integer&gt; partA = Arrays.asList(1, 4, 9, 16);
List &lt;Integer&gt; partB = Arrays.asList(9, 7, 4, 9, 11);
System.out.println(&quot;A: &quot; + partA);
System.out.println(&quot;B: &quot; + partB);
List &lt;Integer&gt; partAB = new ArrayList &lt;Integer&gt;();
partAB.addAll(partA);
partAB.addAll(partB);
System.out.println(&quot;Append: &quot; + partAB);
}

Output:

A: [1, 4, 9, 16]
B: [9, 7, 4, 9, 11]
Append: [1, 4, 9, 16, 9, 7, 4, 9, 11]

答案2

得分: 0

如果您的最终结果应该是:

结果应该为 [1, 4, 9, 16, 9, 7, 4, 9, 11]

前半部分的值来自 PartA 列表,然后后半部分全部来自 PartB 列表。因此,您需要首先将所有 PartA 的值添加到您的 'appended' 列表中,然后再添加 PartB 的值。

基于这一点,您的 append 方法需要更改为:

public static ArrayList<Integer> append(ArrayList<Integer> partA, ArrayList<Integer> partB)
{
     ArrayList<Integer> append = new ArrayList<Integer>();

    for(int x = 0; x < partA.size(); x++)  
    {
        append.add(partA.get(x));
    }

    for(int x = 0; x < partB.size(); x++)  
    {
        append.add(partB.get(x));
    }
    return append;
}

然后只需在附加的数组列表上进行迭代,它将按您指定的顺序进行打印。

更新:

更简单的方法是按照另一篇帖子中所说的,您可以使用 addAll 方法将前两个 ArrayList 的值添加到附加的数组列表中。

英文:

If your end result is supposed to be:

> The result should be [1, 4, 9, 16, 9, 7, 4, 9, 11]

The first half of the values is from the PartA list then the second half is all from the PartB list. So you need to first add all the PartA values first to your 'appended' list, then you'd add the PartB values.

With that in mind your append method needs to be changed to:

public static ArrayList&lt;Integer&gt; append(ArrayList &lt;Integer&gt; partA, ArrayList &lt;Integer&gt; partB)
{
ArrayList&lt;Integer&gt; append = new ArrayList&lt;Integer&gt;();
for(int x = 0; x &lt; partA.size(); x++)  
{
append.add(partA.get(x));
}
for(int x = 0; x &lt; partB.size(); x++)  
{
append.add(partB.get(x));
}
return append;
}
}

Then simply iterate over the appended array list and it'll print it out in the order you specified.

Updated:

simpler way is to do what the other post says and you could use the addAll method to add the values of the first two ArrayLists to the appended array list.

答案3

得分: 0

import java.util.ArrayList;

public class practice_1 {

    public static void main(String[] args) {
        /**10/3/2020
         * Appends one array list to another
         */

        ArrayList<Integer> partA = new ArrayList<Integer>();
        partA.add(1);
        partA.add(4);
        partA.add(9);
        partA.add(16);
        ArrayList<Integer> partB = new ArrayList<Integer>();
        partB.add(9);
        partB.add(7);
        partB.add(4);
        partB.add(9);
        partB.add(11);



        System.out.println("A: " + partA);
        System.out.println("B: " + partB);
        partA.addAll(partB);
        System.out.println("Append: " + partA); //How do I print the two ArrayLists together?
    }

}
英文:
import java.util.ArrayList;
public class practice_1 {
public static void main(String[] args) {
/**10/3/2020
* Appends one array list to another
*/
ArrayList &lt;Integer&gt; partA = new ArrayList &lt;Integer&gt;();
partA.add(1);
partA.add(4);
partA.add(9);
partA.add(16);
ArrayList &lt;Integer&gt; partB = new ArrayList &lt;Integer&gt;();
partB.add(9);
partB.add(7);
partB.add(4);
partB.add(9);
partB.add(11);
System.out.println(&quot;A: &quot; + partA);
System.out.println(&quot;B: &quot; + partB);
partA.addAll(partB);
System.out.println(&quot;Append: &quot; + partA); //How do I print the two ArrayLists together?
}
}

答案4

得分: 0

使用流(Streams)

List<String> newList = Stream.of(listOne, listTwo).flatMap(Collection::stream).collect(Collectors.toList());

使用流(Streams)合并

List<String> newList = Stream.concat(listOne.stream(), listTwo.stream()).collect(toList());

使用Apache commons-collections

List<String> newList = ListUtils.union(listOne, listTwo);

最后,你可以使用以下代码来打印结果:

System.out.println(newList);
英文:

There are multiple ways to combine two lists.

Using Streams

 List&lt;String&gt; newList = Stream.of(listOne, listTwo).flatMap(Collection::stream).collect(Collectors.toList());

Using Streams concat

List&lt;String&gt; newList = Stream.concat(listOne.stream(), listTwo.stream()).collect(toList());

Using Apache commons-collections

List&lt;String&gt; newList = ListUtils.union(listOne, listTwo);

Finally to print you can use

System.out.println(newList)

huangapple
  • 本文由 发表于 2020年10月3日 23:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64185818.html
匿名

发表评论

匿名网友

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

确定