Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:17:31+00:00 2026-06-15T09:17:31+00:00

Is an ArrayList<ArrayList<Integer>> numbers; like a 2D array of ints int[][] numbers; ? Or

  • 0

Is an
ArrayList<ArrayList<Integer>> numbers; like a 2D array of ints int[][] numbers;?
Or are these stored completely different from one another?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:17:32+00:00Added an answer on June 15, 2026 at 9:17 am

    It is a structure with two dimensions, so it is [][]-like, yet there is one very important difference: if you allocate a two-dimensional array in one step you’ll get same size in the second dimension for all elements of the first dimension:

    int[][] arrayOfInts = new int[5][4];
    
    for (int[] second : arrayOfInts) {
       System.out.println(second.length);
    }
    

    prints 5 times a “4”.

    Using ArrayList of ArrayLists all elements of the second dimension may have different size.

    As pointed out by jlordo: an array of ints may also have the second dimension of different lengths if it is created dynamically:

    int[][] anotherArray = new int[5][];
    
    for (int i=0; i<5; i++) {
      anotherArray[i] = new int[i];
    }
    

    in which case a NullPointerException can be throws if the second dimension were accessed before being intialized, like:

    int[][] yetAnotherArray = new int[5][];
    System.out.println(yetAnotherArray[2][3]);
    

    The other difference: after allocating a int[x][y] the memory for all its elements in both dimension is allocated from the very first moment. In ArrayList of ArrayLists the memory needed for the lists is allocated, but the memory needed for its elements will be used not before you create their content. So a similar code as before will print nothing, because at the beginning there will not be even a single element in the first ArrayList.

    In order to have the second dimension you first have to create all ArrayLists of the second dimension:

    ArrayList<ArrayList<Integer>> arrayOfArrays = new ArrayList<ArrayList<Integer>>();
    for (int i=0; i < 5; i++) {
        arrayOfArrays.add(new ArrayList<Integer>();
    }
    

    Further on the accessing side:

    int[][] arrayOfInts = new int[5][4];
    System.out.println(arrayOfInts[2][3]);
    

    prints 0 because all of the memory is already allocated. The access is safe both in addressing the dimensions and its value, because it is of a primitive type.

    ArrayList<ArrayList<Integer>> arrayOfArrays = new ArrayList<ArrayList<Integer>>();
    for (int i=0; i < 5; i++) {
        arrayOfArrays.add(new ArrayList<Integer>();
    }
    System.out.println(first.get(2).get(3));
    

    throws ArrayOutOfBoundsException. You have to check the size of the elements before accessing them.

    Now there is also one important difference between int[][] and Integer[][]: primitive types always have values, so after allocating int[4][5] you’ll have 0 for unitialized elements. Integer[4][5] contain objects, so unitialized elements will have null instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i have an ArrayList of type Integer, containing numbers like 1,3,4,9,10 etc... How
I have an Arraylist and an Array of integer like this, ArrayList<Integer> ar= new
I have one class with a method like this: public ArrayList<Integer> myNumbers() { ArrayList<Integer>
Possible Duplicate: list.clear() vs list = new ArrayList<int>(); I have a list: List<Integer> l1
Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
I want to put some numbers from Excel to array. Let's say I have
I'm supposed to be going through an array of these numbers, 7 0 1
Public extreme_foods As New System.Collections.ArrayList() Dim i As Integer i = 1 For Each
I am trying to run the following code: List<Integer> list = Arrays.asList(1,2,3); ArrayList<Integer> val

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.