Create Arraylist from Array

Create ArrayList from array

new ArrayList<>(Arrays.asList(array));

How to create ArrayList (ArrayList<Integer>) from array (int[]) in Java

The problem in

intList = new ArrayList<Integer>(Arrays.asList(intArray));

is that int[] is considered as a single Object instance since a primitive array extends from Object. This would work if you have Integer[] instead of int[] since now you're sending an array of Object.

Integer[] intArray = new Integer[] { 0, 1 };
//now you're sending a Object array
intList = new ArrayList<Integer>(Arrays.asList(intArray));

From your comment: if you want to still use an int[] (or another primitive type array) as main data, then you need to create an additional array with the wrapper class. For this example:

int[] intArray = new int[] { 0, 1 };
Integer[] integerArray = new Integer[intArray.length];
int i = 0;
for(int intValue : intArray) {
integerArray[i++] = intValue;
}
intList = new ArrayList<Integer>(Arrays.asList(integerArray));

But since you're already using a for loop, I wouldn't mind using a temp wrapper class array, just add your items directly into the list:

int[] intArray = new int[] { 0, 1 };
intList = new ArrayList<Integer>();
for(int intValue : intArray) {
intList.add(intValue);
}

How can I create an Array of ArrayLists?

As per Oracle Documentation:

"You cannot create arrays of parameterized types"

Instead, you could do:

ArrayList<ArrayList<Individual>> group = new ArrayList<ArrayList<Individual>>(4);

As suggested by Tom Hawting - tackline, it is even better to do:

List<List<Individual>> group = new ArrayList<List<Individual>>(4);

Initialization of an ArrayList in one line

Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way:

ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");

The catch is that there is quite a bit of typing required to refer to that list instance.

There are alternatives, such as making an anonymous inner class with an instance initializer (also known as an "double brace initialization"):

ArrayList<String> list = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};

However, I'm not too fond of that method because what you end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object -- that just seems like a little bit overkill to me.

What would have been nice was if the Collection Literals proposal for Project Coin was accepted (it was slated to be introduced in Java 7, but it's not likely to be part of Java 8 either.):

List<String> list = ["A", "B", "C"];

Unfortunately it won't help you here, as it will initialize an immutable List rather than an ArrayList, and furthermore, it's not available yet, if it ever will be.

Create ArrayList from array

new ArrayList<>(Arrays.asList(array));

Creating arraylist of arrays

The problem is that you are adding the same object to each index of your ArrayList. Every time you modify it, you are modifying the same object. To solve the problem, you have to pass references to different objects.

String[] t2 = new String[2]; 

ArrayList<String[]> list2 = new ArrayList<String[]>();

t2[0]="0";
t2[1]="0";
list2.add(t2);

t2 = new String[2]; // create a new array
t2[0]="0";
t2[1]="1";
list2.add(t2);

t2 = new String[2];
t2[0]="1";
t2[1]="0";
list2.add(t2);

t2 = new String[2];
t2[0]="1";
t2[1]="1";
list2.add(t2);

Convert an array into an ArrayList

As an ArrayList that line would be

import java.util.ArrayList;
...
ArrayList<Card> hand = new ArrayList<Card>();

To use the ArrayList you have do

hand.get(i); //gets the element at position i 
hand.add(obj); //adds the obj to the end of the list
hand.remove(i); //removes the element at position i
hand.add(i, obj); //adds the obj at the specified index
hand.set(i, obj); //overwrites the object at i with the new obj

Also read this http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

Convert String array to ArrayList

Use this code for that,

import java.util.Arrays;  
import java.util.List;
import java.util.ArrayList;

public class StringArrayTest {

public static void main(String[] args) {
String[] words = {"ace", "boom", "crew", "dog", "eon"};

List<String> wordList = Arrays.asList(words);

for (String e : wordList) {
System.out.println(e);
}
}
}

Create ArrayList of arrays using Arrays.asList

I think you want to create a List<String[]> using Arrays.asList, containing the string array {"Slot"}.

You can do that like this:

List<String[]> notWithArrays = Arrays.asList(new String[][] {{"Slot"}});

or you can explicitly specify the generic type parameter to asList, like this:

List<String[]> notWithArrays = Arrays.<String[]>asList(new String[] {"Slot"});

How to declare an ArrayList with values?

In Java 9+ you can do:

var x = List.of("xyz", "abc");
// 'var' works only for local variables

Java 8 using Stream:

Stream.of("xyz", "abc").collect(Collectors.toList());

And of course, you can create a new object using the constructor that accepts a Collection:

List<String> x = new ArrayList<>(Arrays.asList("xyz", "abc"));

Tip: The docs contains very useful information that usually contains the answer you're looking for. For example, here are the constructors of the ArrayList class:

  • ArrayList()

    Constructs an empty list with an initial capacity of ten.

  • ArrayList(Collection<? extends E> c) (*)

    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

  • ArrayList(int initialCapacity)

    Constructs an empty list with the specified initial capacity.



Related Topics



Leave a reply



Submit