简体   繁体   English

将三个数组列表中的元素添加到Java中的另一个列表的有效方法?

[英]efficient way to add the elements from three array lists to another list in java?

Suppose I have three arrayList list1, list2 and list3. 假设我有三个arrayList list1,list2和list3。 This is what I do: 这是我的工作:

list1.addAll(list2).addAll(list3);

But I got a "boolean can not be dereferenced" error. 但是我遇到了“布尔值无法取消引用”错误。 Does any know why? 不知道为什么吗? Thanks very much. 非常感谢。

Take a look at the method documentation 看看方法文档

public boolean addAll(Collection<? extends E> c)

Which means the addAll() returns a boolean value. 这意味着addAll()返回一个布尔值。

When you concat two addAll() calls together, you get that error. 当您同时合并两个addAll()调用时,会收到该错误。

It could be easily avoided by doing the addAll() separately. 通过单独执行addAll()可以轻松避免这种情况。

list1.addAll(list2);
list1.addAll(list3);

The addAll() method returns a boolean indicating whether the target collection changed as a result of the call. addAll()方法返回一个布尔值,该布尔值指示目标集合是否由于调用而改变。 You're sending the second addAll() to the results of the first addAll(), which is a boolean. 您将第二个addAll()发送到第一个addAll()的结果,这是一个布尔值。 You want: 你要:

list1.addAll(list2);
list1.addAll(list3);

well the below practice could also work for an example.. 下面的做法也可以作为示例。

/*
  Copy Elements of One Java ArrayList to Another Java ArrayList Example
  This java example shows how to copy all elements of one Java ArrayList object to
  another Java ArrayList object using copy method of Collections class.
*/

import java.util.ArrayList;
import java.util.Collections;

public class CopyElementsOfArrayListToArrayListExample {

  public static void main(String[] args) {

    //create first ArrayList object
    ArrayList arrayList1 = new ArrayList();

    //Add elements to ArrayList
    arrayList1.add("1");
    arrayList1.add("2");
    arrayList1.add("3");

    //create another ArrayList object
    ArrayList arrayList2 = new ArrayList();

    //Add elements to Arraylist
    arrayList2.add("One");
    arrayList2.add("Two");
    arrayList2.add("Three");
    arrayList2.add("Four");
    arrayList2.add("Five");

    /*
      To copy elements of one Java ArrayList to another use,
      static void copy(List dstList, List sourceList) method of Collections class.

      This method copies all elements of source list to destination list. After copy
      index of the elements in both source and destination lists would be identical.

      The destination list must be long enough to hold all copied elements. If it is
      longer than that, the rest of the destination list's elments would remain
      unaffected.      
    */

    System.out.println("Before copy, Second ArrayList Contains : " + arrayList2);

    //copy all elements of ArrayList to another ArrayList using copy
    //method of Collections class
    Collections.copy(arrayList2,arrayList1);

    /*
      Please note that, If destination ArrayList object is not long
      enough to hold all elements of source ArrayList,
      it throws IndexOutOfBoundsException.
    */

    System.out.println("After copy, Second ArrayList Contains : " + arrayList2);  
  }
}

/*
Output would be
Before copy, Second ArrayList Contains : [One, Two, Three, Four, Five]
After copy, Second ArrayList Contains : [1, 2, 3, Four, Five]
*/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从数组列表中删除元素的更有效方法 - More efficient way to remove elements from an array list 从数组中删除连续元素的最有效方法? 爪哇 - Most efficient way to remove consectuive elements from an array? Java Java:通过转换将元素从列表添加到另一个列表 - Java: add elements from a list to another with conversion 使用其他列表中的单独元素和元素填充java列表的最佳方法是什么? - What best way to populate java list with separate elements and elements from other lists? 将元素从一个ArrayList添加到另一ArrayList到特定位置的最有效方法(根据RAM / CPU消耗) - Most efficient way (according RAM/CPU consumption) to add elements from one ArrayList to another ArrayList to specific position 从Java列表中删除和添加元素的最佳方法 - Best way to remove and add elements from the java List 通过与 Java 中的另一个数组进行比较,从对象列表中删除元素 - Remove elements from List of objects by comparing with another array in Java Java - 从Array []中删除一组元素的最有效方法是什么 - Java - What's the most efficient way of removing a set of elements from an Array[] 如何在java中以有效的方式将数组中的元素写入我的哈希映射? - how can I write elements from my array to my hash map in efficient way in java? Java:有没有更有效的方法将 JFrame 元素写入数组 - Java: Is there a more efficient way to write JFrame elements into an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM