Java size of ArrayList of Arraylist - java

I have created an ArrayList A each element of which further contains an ArrayList. All entries are integers. I wish to then calculate the length of "outer" ArrayList.
ArrayList<Integer>[] A = (ArrayList<Integer>[])new ArrayList[n];
for (int i = 0; i < n; i++) {
A[i] = new ArrayList<Integer>();
}
How can I use the size() or length() functions to obtain the result n?
Directly using A.size() gives the error error: cannot find symbol. This is required because I'm passing A to other functions and do not want to pass n as well every time.

I have created an ArrayList A
Actually, you just created an array of ArrayLists.
A.length gets you the size of that
A[index].size() gets you the sizes of any one list.
If you wanted an ArrayList called A
ArrayList<ArrayList<Integer>> A = new ArrayList<>();

You should use A[i].size() to obtain size of each array list

How can I use the size() or length() functions to obtain the result n?
The identifier A is a reference to an array not ArrayList. The Array A contains ArrayLists.
below shows how to access the length:
ArrayList<Integer>[] A = (ArrayList<Integer>[])new ArrayList[10];
for (int i = 0; i < 10; i++) {
A[i] = new ArrayList<Integer>();
}
System.out.println(A.length);

Related

Can i declare an Array once and through out the code declare it again?

Can I re-declare an Array that is already declared?
So I am trying to go through a LinkedList and get every index which includes "null" as an Element and add those indexes to an array of ints.
The problem I have is : The array is already declared as:
int[] solution = new int[0];
Can i redeclare it once again like lets say:
int newSize = 10;
solution = [newSize];
Does that work?
int k = 0;
int counter = 0;
if(!isEmpty())
{
for(int j = 0 ; j < size(); j++)
{
if(current.getContent().equals(null))
{
counter++;
}
}
result = new int[counter];
for(int i = 0 ; i < size(); i++)
{
if(current.getContent().equals(null))
{
result[k++] = i ;
}
}
}
I tried printing out the elements of Result but all i get is well... an empty array.
Short answer (as mentionned on java documentation => link)
The length of an array is established when the array is created. After creation, its length is fixed.
Some more details:
When you use :
int[] solution = new int[0]
you create an array that can hold 0 element and ask "solution" to refert to it.
If later on your code you use solution = new int[10] you will create an array that can hold 10 elements and ask "solution" to refer this new array.
The previous array still exists somewhere in the memory.
Search for "java memory management" if you want a full explanation.

How do I loop thorough a 2D ArrayList in Java and fill it?

I am trying to use 2D arrayLists in Java.
I have the definition:
ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>();
How can I loop through it and enter in numbers starting from 1?
I know that I can access a specific index by using:
myList.get(i).get(j)
Which will get the value. But how do I add to the Matrix?
Thanks
You can use a nested for loop. The i-loop loops through the outer ArrayList and the j-loop loops through each individual ArrayList contained by myList
for (int i = 0; i < myList.size(); i++)
{
for (int j = 0; j < myList.get(i).size(); j++)
{
// do stuff
}
}
Edit: you then fill it by replacing // do stuff with
myList.get(i).add(new Integer(YOUR_VALUE)); // append YOUR_VALUE to end of list
A Note: If the myList is initially unfilled, looping using .size() will not work as you cannot use .get(SOME_INDEX) on an ArrayList containing no indices. You will need to loop from 0 to the number of values you wish to add, create a new list within the first loop, use .add(YOUR_VALUE) to append a new value on each iteration to this new list and then add this new list to myList. See Ken's answer for a perfect example.
Use for-each loop, if you are using Java prior 1.5 version.
for(ArrayList<Integer> row : myList) {
for(Integer intValue : row) {
// access "row" for inside arraylist or "intValue" for integer value.
}
}
Assuming the matrix is not initialized,
int m = 10, n = 10;
ArrayList<ArrayList<Integer>> matrix = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < m; i++) {
List<Integer> row = new ArrayList<Integer>();
for (int j = 0; j < n; j++) {
row.add(j);
}
matrix.add(row);
}

Populating ArrayList

I am trying to populate an array list, however, my array list constantly equals 0, and never initializes, despite my declaring it over main().
This is my code.
static ArrayList<Integer> array = new ArrayList<Integer>(10); //The parenthesis value changes the size of the array.
static Random randomize = new Random(); //This means do not pass 100 elements.
public static void main (String [] args)
{
int tally = 0;
int randInt = 0;
randInt = randomize.nextInt(100); //Now set the random value.
System.out.println(randInt); //Made when randomizing number didn't work.
System.out.println(array.size() + " : Array size");
for (int i = 0; i < array.size(); i++)
{
randInt = randomize.nextInt(100); //Now set the random value.
array.add(randInt);
tally = tally + array.get(i); //add element to the total in the array.
}
//System.out.println(tally);
}
Can someone tell me what is going on? I feel rather silly, I've done ArrayLists for my default arrays and I cannot figure this out to save my life!
new ArrayList<Integer>(10) creates an ArrayList with initial capacity of 10 but the size is still 0 as there are no elements in it.
ArrayList is backed by an array underneath so it does create an array of a given size (initial capacity) when constructing the object so it doesn't need to resize it every time you insert a new entry (arrays in Java are not dynamic so when you want to insert a new record and the array is full you need to create a new one and move all the items, that's an expensive operation) but even though the array is created ahead of time size() will return 0 until you actually add() something to the list.
That's why this loop:
for (int i = 0; i < array.size(); i++) {
// ...
}
Will not execute as array.size() is 0.
Change it to:
for (int i = 0; i < 10; i++)
And it should work.

Clean way to initialize an arraylist

I want an Arraylist in Java, which I want to fill with 10's
ArrayList<Integer> list = new ArrayList<Integer>(100);
for (int i = 0; i < 100; i++) {
list.add(10);
}
I'm going to have to initialize a lot of Arraylists, so I was wondering if there is there a clean way to do this without a for loop?
You can use Collections.nCopies:
ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(100, 10));
This will initialize list with 100 10's.
ArrayList<Integer> list = new ArrayList<Integer>(100);
for (int i = 0; i < list.size(); i++)
{
list.add(10);
}
list.size() will be 0 here, so this is why your code does not work. size keeps track of how many elements are currently in the list, not the capacity.
If you want the ArrayList to be initialized with all 10s, you can use:
ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(100, 10));
Edit: You said later that you didn't want a for loop, but to fix your code, just replace list.size() with 100.

Java array with empty second brackets

In Java you can do this
int[][] i = new int[10][];
Does this just create 10 empty arrays of int? Does it have other implications?
It creates a 10-entry array of int[]. Each of those 10 array references will initially be null. You'd then need to create them (and because Java doesn't have true multidimensional arrays, each of those 10 int[] entries can be of any length).
So for instance:
int i[][] = new int [10][];
i[0] = new int[42];
i[1] = new int[17];
// ...and so on
Executing your code creates an array of size 10, each element of which can hold a reference to a int[], but which are all initialized to null.
In order to use the int[]s, you would have to create new int[] for each of the element, something like this:
for (int n = 0; n < 10; n++)
i[n] = new int[10]; // make them as large as you need
Yes, it does; however, each of those arrays are null. You have to then initialize each of those sub-arrays, by saying int[10][0] = new int[MY_SIZE], or something similar. You can have arrays with different lengths inside the main array; for example, this code would work:
int[][] i = new int[10][];
for(int ind = 0; ind<10;ind++){
i[ind]=new int[ind];
}
It is just an array of arrays.
Here you create ten new int[0] arrays. You have to manually initialize it, it's useful when you don't need square matrix:
int[][] array = new int[10][];
for (int i = 0; i < array.length; i++) {
array[i] = new int[i];
}
If you need square matrix you can do:
int[][] array = new int[10][10];
And it will be initialized with default values.
That is just the declaration, you need to initialize it. The 10 arrays would be null initially.

Categories

Resources