4

Can I insert array to array and read it afterwards in Java? Something like this:

ArrayList<Integer> array = new ArrayList<>();
array.add([1,4,5]);
array.add([3,7,2]);

Array:

{ [1,4,5], [3,7,2] }

And read like:

print(array[0][1]); //prints 4 

Thanks.

4
  • Not directly, but it wouldn't take much (IMO) to write the code for it Commented Jun 30, 2016 at 18:20
  • arraylist is a list, not array. they are different thing. you may want to create a 2d array Commented Jun 30, 2016 at 18:24
  • 1
    I can't imagine any sensible Java code that would print 4 at [1][2] when arrays you have shown are added (even if you use some 2d array type like array of arrays) Commented Jun 30, 2016 at 18:25
  • Sorry for mistake, I'm human only... Commented Jun 30, 2016 at 18:35

6 Answers 6

7

You could write ArrayList<int[]> and pass values like .add(new int[]{...}).

For example,

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

To print values out you should get an int[] array firstly by get(int index) and then get a value from this array by [index]:

System.out.print(list.get(0)[0]); // prints 1

About the mess in the comments.

  1. There are no raw types here. I didn't write, for example, List or List<Object> anywhere.
  2. int[] is an array, therefore it is a reference type and can be used as a generic parameter.
  3. int is a primitive which doesn't work/use with generics. Consider its wrapper class - Integer.
  4. I used the diamond <> above. Why? To prevent "boilerplate code".

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond.
List<int[]> list = new ArrayList<>();
List<int[]> list = new ArrayList<int[]>(); is equivalent to the previous

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

6 Comments

@Berdesdan I think you're mixing up terms. int is a primitive type, not a raw type.
fixed your -1, i dont get this community sometimes. your answer is completely fine
@SotiriosDelimanolis spot on - "A raw type is the name of a generic class or interface without any type arguments"
@AndrewTobilko sorry about the confusion, you are not wrong. I was confused about raw/primitive types. Jeutnarg's link cleared it up..
@Berdesdan And even that is incorrect. The diamond operator <> is ok here.
|
3

to quickly fill an arrayList with fewest lines of code :

    Integer[] numArray = {1, 2, 3, 4, 5, 0, 9, 8, 7, 6};
    List<Integer> numList = new ArrayList<>(Arrays.asList(numArray));

Comments

1

You can add integer arrays to an arraylist, but then the arraylist must be defined as:

List<int[]> list = new ArrayList<int[]>();

In Fact a more generic version would be:

List<Object> list = new ArrayList<Object>();

The array.add([1,4,5]); is implemented correctly as

list.add(new int[]{1,4,5});

The print(array[1][2]); is implemented correctly as:

System.out.println(((int [])list.get(1))[2]);

3 Comments

Why would you use a more generic version when you know int[] is the type you want to use?
I never said that you must use it, I just wanted the OP to be aware of it.
But if anything, it's just going to confuse OP more. IMO it clutters up your answer, but it's not a big deal.
0

ArrayList is List type.

If you want to create 2d array (array inside array)

int[][] a = new int[][]{{1,4,5},{3,7,2}};
System.out.println(a[0][1]);

Comments

0

You want to use ArrayList of ArrayLists

    ArrayList<ArrayList<Integer>> intArrays = new ArrayList<ArrayList<Integer>>();
    //rows
    for(int i =0;i<4;i++)
    {

        intArrays.add(new ArrayList<Integer>());
        //cols
        for(int j=0;j<4;j++)
        {
            intArrays.get(i).add(j);
        }
    }
    //rows
    for(int i =0;i<4;i++)
    {

        //cols
        for(int j=0;j<4;j++)
        {
            System.out.print(intArrays.get(i).get(j));
        }
        System.out.println();
    }

Comments

-1

This has been tested and is working.

    int[] ar = {10, 20, 20, 10, 10, 30, 50, 10, 20};

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

    for(int i:ar){
        list.add(new Integer(i));

    }
    System.out.println(list.toString());

    // prints : [10, 20, 20, 10, 10, 30, 50, 10, 20]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.