简体   繁体   中英

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

Suppose I have three arrayList list1, list2 and 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.

When you concat two addAll() calls together, you get that error.

It could be easily avoided by doing the addAll() separately.

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

The addAll() method returns a boolean indicating whether the target collection changed as a result of the call. You're sending the second addAll() to the results of the first addAll(), which is a boolean. 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]
*/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM