Which one of the following is the correct way to declare an ArrayList of integers

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].

Which one of the following is the correct way to declare an ArrayList of integers

asked May 7, 2012 at 6:26

2

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.

answered May 7, 2012 at 6:34

Raul ReneRaul Rene

9,8639 gold badges52 silver badges75 bronze badges

0

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 (...)

Wahib Ul Haq

4,0143 gold badges44 silver badges40 bronze badges

answered Jun 15, 2017 at 11:03

1

answered May 7, 2012 at 6:28

truthealitytrutheality

22.6k6 gold badges51 silver badges67 bronze badges

1

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!!

answered Nov 30, 2017 at 15:50

mtebongmtebong

1111 silver badge3 bronze badges

3

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);
    }

answered May 7, 2012 at 6:37

EveryoneEveryone

2,3462 gold badges26 silver badges38 bronze badges

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));
}

Which one of the following is the correct way to declare an ArrayList of integers

mayo

3,6851 gold badge35 silver badges42 bronze badges

answered Oct 18, 2015 at 15:57

Which one of the following is the correct way to declare an ArrayList of integers

NourNour

1,4281 gold badge18 silver badges27 bronze badges

3

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.

answered Mar 31, 2014 at 22:45

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

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

Which one of the following is the correct way to declare an ArrayList of integers

clemens

16k11 gold badges45 silver badges62 bronze badges

answered Aug 6, 2017 at 9:41

Rahul NaikRahul Naik

1051 silver badge8 bronze badges

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);

Which one of the following is the correct way to declare an ArrayList of integers

Maddy

4,1154 gold badges32 silver badges53 bronze badges

answered Sep 7, 2017 at 11:31

Which one of the following is the correct way to declare an ArrayList of integers

GIGOGIGO

689 bronze badges

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

answered Jan 11, 2020 at 6:49

Which one of the following is the correct way to declare an ArrayList of integers

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.

answered Jun 30, 2020 at 13:46

Which one of the following is the correct way to declare an ArrayList of integers

Anshuman KumarAnshuman Kumar

3621 gold badge5 silver badges19 bronze badges

List<Integer> integerList = IntStream.range(0,100)
   .boxed()
   .toList();

This is one of the ways, you can initialize the fixed size ArrayList in Java using Java8+ - Stream API. integerList is going to contain integer values from 0 to 99.

Which one of the following is the correct way to declare an ArrayList of integers

Sha

76110 silver badges39 bronze badges

answered Aug 10 at 6:18

Which one of the following is the correct way to declare an ArrayList of integers

1