1

I have a problem in converting from Array to ArrayList

public class one 
{
public static void main(String args[])
{
int y[]={12,25,38,46};
two p=new two();
p.setLocations(y);
}
}


import java.io.*;
import java.util.*;   

public class two 
{
    ArrayList<Integer> data_array=new ArrayList<Integer>();


void setLocations(int locations[])
{
        ArrayList<Integer> locations_arraylist=new ArrayList(Arrays.asList(locations));
        data_array=locations_arraylist;
    for(int i=0;i<data_array.size();i++)
        System.out.println("data_array["+i+"]="+data_array.get(i));
}
}

In the below line

ArrayList<Integer> locations_arraylist=new  ArrayList(Arrays.asList(locations));
//Copying from array to ArrayList-Its converting,Please suggest
3
  • What is locations: array of int or ArrayList<Integer>? Please make up your mind. Commented Nov 8, 2015 at 22:32
  • @Eng.Fouad "locations" is an array of int,I needed to convert locations from int into ArrayList<Integer> Commented Nov 8, 2015 at 22:37
  • Does this answer your question? How to convert int[] into List<Integer> in Java? Commented Jul 15, 2022 at 20:59

5 Answers 5

4

An int[] is quite different from a List<Integer>. For example, an Integer has an identity as well as a value. There is no very simple way to do the conversion.

The following way works with Java 8.

int[] array = {1, 2, 3, 4, 5};
List<Integer> list = IntStream.of(array).boxed().collect(Collectors.toCollection(ArrayList::new));

The following way works in earlier versions.

int[] array = {1, 2, 3, 4, 5};
List<Integer> list = new ArrayList<Integer>();
for (int a : array)
    list.add(a);

If you pass an int[] to Arrays.asList you get a List<int[]>, not a List<Integer>.

Sign up to request clarification or add additional context in comments.

1 Comment

refer this answer. It contains additional details.
0

You can try this:

replace int y[]={12,25,38,46}; to Integer y[] = {12, 25, 38, 46};
No need of this line as well
ArrayList<Integer> data_array = new ArrayList<Integer>();
You can use for-each loop for print the array :

int i=0;
        for (Integer locations_arraylist1 : locations_arraylist) {
             System.out.println("data_array[" + i + "]=" + locations_arraylist1);
             i++;
        }

3 Comments

setLocations(Integer locations[]) should be an Integer array parameter as well .
i need to extend my class and needed to perform add and remove options in future.
Then Its better to go with Paul Boddington 's answer . He has used a list for his solution there. I was just trying to make minimal fixes and make ur code work . :)
0

It is recommended to use the interface List for members/variables and use the ArrayList constructor. Also ArrayList without the brackets indicates the raw type instead of instead of the generic.

If you would like to avoid a for loop that will copy the values from array to List, there are 2 solutions :

  1. Guava (put on top import com.google.common.primitives.Ints;)

    List<Integer> locations_arraylist = Ints.asList(locations);
    
  2. pass the values to Arrays.asList() directly

    List<Integer> locations_arraylist = Arrays.asList(12, 25, 38, 46);
    

Comments

0

Try this:

int[] arrayInt = new int[]{1, 2, 3};
List<Integer> list = Arrays.asList(ArrayUtils.toObject(arrayInt));

Comments

0

Int array can be converted to arraylist in the following way:

int[] intArray = new int[]{100,200,300};
Integer[] integerArray = Arrays.stream( intArray ).boxed().toArray( Integer[]::new );
List<Integer> arrayList = new ArrayList<>( Arrays.asList( integerArray ) );

Similarly, ArrayList can be converted into int array in the following way:

int[] intArray = arrayList.stream().mapToInt( Integer::intValue ).toArray();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.