ArrayList of int array in java

JavaArraylist

Java Problem Overview


I'm new to the concept of arraylist. I've made a short program that is as follows:

ArrayList<int[]> arl=new ArrayList<int[]>();
int a1[]={1,2,3};
arl.add(0,a1);
System.out.println("Arraylist contains:"+arl.get(0));

It gives the output: Arraylist contains:[I@3e25a5

Now my questions are:

  1. How to display the correct value i.e. 1 2 3.
  2. How can I access the single element of array a1 i.e. if I want to know the value at a1[1].

Java Solutions


Solution 1 - Java

First of all, for initializing a container you cannot use a primitive type (i.e. int; you can use int[] but as you want just an array of integers, I see no use in that). Instead, you should use Integer, as follows:

ArrayList<Integer> arl = new ArrayList<Integer>();

For adding elements, just use the add function:

arl.add(1);  
arl.add(22);
arl.add(-2);

Last, but not least, for printing the ArrayList you may use the build-in functionality of toString():

System.out.println("Arraylist contains: " + arl.toString());  

If you want to access the i element, where i is an index from 0 to the length of the array-1, you can do a :

int i = 0; // Index 0 is of the first element
System.out.println("The first element is: " + arl.get(i));

I suggest reading first on Java Containers, before starting to work with them.

Solution 2 - Java

More simple than that.

List<Integer> arrayIntegers = new ArrayList<>(Arrays.asList(1,2,3));
    		
arrayIntegers.get(1);

In the first line you create the object and in the constructor you pass an array parameter to List.

In the second line you have all the methods of the List class: .get (...)

Solution 3 - Java

  1. Use Arrays.toString( arl.get(0) ).

  2. arl.get(0)[1]

Solution 4 - Java

The setup:

    List<int[]> intArrays=new ArrayList<>();
    int anExample[]={1,2,3};
    intArrays.add(anExample);

To retrieve a single int[] array in the ArrayList by index:

    int[] anIntArray = intArrays.get(0); //'0' is the index
    //iterate the retrieved array an print the individual elements
    for (int aNumber : anIntArray ) { 
        System.out.println("Arraylist contains:" + aNumber );
    }

To retrieve all int[] arrays in the ArrayList:

    //iterate the ArrayList, get and print the elements of each int[] array  
    for(int[] anIntArray:intArrays) {
       //iterate the retrieved array an print the individual elements
       for (int aNumber : anIntArray) {
           System.out.println("Arraylist contains:" + aNumber);
       }
}

Output formatting can be performed based on this logic. Goodluck!!

Solution 5 - Java

In java, an array is an object. Therefore the call to arl.get(0) returns a primitive int[] object which appears as ascii in your call to System.out.

The answer to your first question is therefore

System.out.println("Arraylist contains:"+Arrays.toString( arl.get( 0 ) ) );

If you're looking for particular elements, the returned int[] object must be referenced as such. The answer to your second question would be something like

    int[] contentFromList = arl.get(0);
    for (int i = 0; i < contentFromList.length; i++) {
        int j = contentFromList[i];
        System.out.println("Value at index - "+i+" is :"+j);
    }

Solution 6 - Java

You have to use <Integer> instead of <int>:

int a1[] = {1,2,3};
ArrayList<Integer> arl=new ArrayList<Integer>();
for(int i : a1) {
    arl.add(i);        
    System.out.println("Arraylist contains:" + arl.get(0));
}

Solution 7 - Java

Everyone is right. You can't print an int[] object out directly, but there's also no need to not use an ArrayList of integer arrays.

Using,

Arrays.toString(arl.get(0))

means splitting the String object into a substring if you want to insert anything in between, such as commas.

Here's what I think amv was looking for from an int array viewpoint.

System.out.println("Arraylist contains: " 
    + arl.get(0)[0] + ", " 
    + arl.get(0)[1] + ", " 
    + arl.get(0)[2]);

This answer is a little late for amv but still may be useful to others.

Solution 8 - Java

java.util.Arrays.toString() converts Java arrays to a string:

System.out.println("Arraylist contains:"+Arrays.toString(arl.get(0)));

Solution 9 - Java

ArrayList<Integer> list = new ArrayList<>();
int number, total = 0;
	
for(int i = 0; i <= list.size(); i++){
	System.out.println("Enter number " + (i + 1) + " or enter -1 to end: ");
	number = input.nextInt();
	
	list.add(number);
		
	if(number == -1){
		list.remove(list.size() - 1);
		break;
	}
}
System.out.println(list.toString());
	
for(int i: list){
	System.out.print(i + "  ");
	total+= i;
}
System.out.println();
System.out.println("The sum of the array content is: " + total);

Solution 10 - Java

Integer is wrapper class and int is primitive data type.Always prefer using Integer in ArrayList.

Solution 11 - Java

For the more inexperienced, I have decided to add an example to demonstrate how to input and output an ArrayList of Integer arrays based on this question here.

    ArrayList<Integer[]> arrayList = new ArrayList<Integer[]>();
    while(n > 0)
    {
        int d = scan.nextInt();
       Integer temp[] = new Integer[d];
        for (int i = 0 ; i < d ; i++)
        {
            int t = scan.nextInt();
            temp[i]=Integer.valueOf(t);
        }
        arrayList.add(temp);
        n--;
    }//n is the size of the ArrayList that has been taken as a user input & d is the size 
    //of each individual array.

     //to print something  out from this ArrayList, we take in two 
    // values,index and index1 which is the number of the line we want and 
    // and the position of the element within that line (since the question
    // followed a 1-based numbering scheme, I did not change it here)

    System.out.println(Integer.valueOf(arrayList.get(index-1)[index1-1]));

Thanks to this answer on this question here, I got the correct answer. I believe this satisfactorily answers OP's question, albeit a little late and can serve as an explanation for those with less experience.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionamvView Question on Stackoverflow
Solution 1 - JavaRaul ReneView Answer on Stackoverflow
Solution 2 - JavaKarel Muñiz PerdomoView Answer on Stackoverflow
Solution 3 - JavatruthealityView Answer on Stackoverflow
Solution 4 - JavamtebongView Answer on Stackoverflow
Solution 5 - JavaEveryoneView Answer on Stackoverflow
Solution 6 - JavaNourView Answer on Stackoverflow
Solution 7 - Javauser2603432View Answer on Stackoverflow
Solution 8 - JavaRahul NaikView Answer on Stackoverflow
Solution 9 - JavaGIGOView Answer on Stackoverflow
Solution 10 - Javapavan kumar sView Answer on Stackoverflow
Solution 11 - JavaAnshuman KumarView Answer on Stackoverflow