Create ArrayList from array

后端 未结 30 1806
遇见更好的自我
遇见更好的自我 2020-11-21 22:29

I have an array that is initialized like:

Element[] array = {new Element(1), new Element(2), new Element(3)};

I would like to convert this

相关标签:
30条回答
  • 2020-11-21 23:01

    Another Java8 solution (I may have missed the answer among the large set. If so, my apologies). This creates an ArrayList (as opposed to a List) i.e. one can delete elements

    package package org.something.util;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class Junk {
    
        static <T> ArrayList<T>  arrToArrayList(T[] arr){
            return Arrays.asList(arr)
                .stream()
                .collect(Collectors.toCollection(ArrayList::new));
        }
    
        public static void main(String[] args) {
            String[] sArr = new String[]{"Hello", "cruel", "world"};
            List<String> ret = arrToArrayList(sArr);
            // Verify one can remove an item and print list to verify so
            ret.remove(1);
            ret.stream()
                .forEach(System.out::println);
        }
    }
    

    Output is...
    Hello
    world

    0 讨论(0)
  • 2020-11-21 23:01

    We can easily convert an array to ArrayList. We use Collection interface's addAll() method for the purpose of copying content from one list to another.

     Arraylist arr = new Arraylist();
     arr.addAll(Arrays.asList(asset));
    
    0 讨论(0)
  • 2020-11-21 23:02

    as all said this will do so

     new ArrayList<>(Arrays.asList("1","2","3","4"));
    

    and the common newest way to create array is observableArrays

    ObservableList: A list that allows listeners to track changes when they occur.

    for Java SE you can try

    FXCollections.observableArrayList(new Element(1), new Element(2), new Element(3));
    

    that is according to Oracle Docs

    observableArrayList() Creates a new empty observable list that is backed by an arraylist. observableArrayList(E... items) Creates a new observable array list with items added to it.

    Update Java 9

    also in Java 9 it's a little bit easy:

    List<String> list = List.of("element 1", "element 2", "element 3");
    
    0 讨论(0)
  • 2020-11-21 23:04
    1. If we see the definition of Arrays.asList() method you will get something like this:

       public static <T> List<T> asList(T... a) //varargs are of T type. 
      

      So, you might initialize arraylist like this:

       List<Element> arraylist = Arrays.asList(new Element(1), new Element(2), new Element(3));
      

      Note : each new Element(int args) will be treated as Individual Object and can be passed as a var-args.

    2. There might be another answer for this question too.
      If you see declaration for java.util.Collections.addAll() method you will get something like this:

      public static <T> boolean addAll(Collection<? super T> c, T... a);
      

      So, this code is also useful to do so

      Collections.addAll(arraylist, array);
      
    0 讨论(0)
  • 2020-11-21 23:05

    Given Object Array:

    Element[] array = {new Element(1), new Element(2), new Element(3) , new Element(2)};
    

    Convert Array to List:

        List<Element> list = Arrays.stream(array).collect(Collectors.toList());
    

    Convert Array to ArrayList

        ArrayList<Element> arrayList = Arrays.stream(array)
                                           .collect(Collectors.toCollection(ArrayList::new));
    

    Convert Array to LinkedList

        LinkedList<Element> linkedList = Arrays.stream(array)
                         .collect(Collectors.toCollection(LinkedList::new));
    

    Print List:

        list.forEach(element -> {
            System.out.println(element.i);
        });
    

    OUTPUT

    1

    2

    3

    0 讨论(0)
  • Another update, almost ending year 2014, you can do it with Java 8 too:

    ArrayList<Element> arrayList = Stream.of(myArray).collect(Collectors.toCollection(ArrayList::new));
    

    A few characters would be saved, if this could be just a List

    List<Element> list = Stream.of(myArray).collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题