0

How would I access an Integer Array inside an Arraylist, both of which are resizable?

I have this code so far:

List<Integer[]> vertices_passed = new ArrayList<Integer[]>();

And I want to go into vertices_passed and add an element to the Integer array.

Something like this:

vertices_passed.get(certain_index).set(index_of_integer_array, Integer.valueof(9))

Is there a better way to use ArrayLists to create a data structure that looks like this:

ArrayList {
    [0]
        [0] A
        [1] B
    [1]
        [0] A
        [1] C
        [2] F
        [3] G
    [2]
        [0] D
        [1] C
        [2] F
        [3] G
        [4] H
    [3]
        [0] A
        [0] D
        [0] I

Also- how would I add elements to the inner array?

Thanks

5
  • 3
    Why don't you use ArrayList<ArrayList<Integer>>? Commented Sep 16, 2013 at 22:11
  • 2
    @CommuSoft you mean List<List<Integer>> backed by ArrayList<List<Integer>> that will contain ArrayList<Integer>. Commented Sep 16, 2013 at 22:12
  • Indeed, however there is nothing wrong with ArrayList<ArrayList<Integer>> it simply puts more constraints on the types used. One can also use an ArrayList<LinkedList<Integer>> Commented Sep 16, 2013 at 22:13
  • It all depends on how you'll need to access the data, if you will always need to read all the elements or if you know the data will always be sorted and you won't need to add any more elements to inner entries then it would be ok, otherwise you could consider ArrayList<ArrayList<Integer>> or Map<Integer, ArraList<Integer>> Commented Sep 16, 2013 at 22:14
  • 1
    Well since the question stated adding an element, a LinkedList can perform this in constant time worst case while an ArrayList could take linear time worst case. Commented Sep 16, 2013 at 22:17

4 Answers 4

2

You could do it like this:

package com.sandbox;


import java.util.ArrayList;
import java.util.List;

public class Sandbox {

    public static void main(String[] args) {
        List<Integer[]> dt = new ArrayList<>();
        dt.add(new Integer[2]);
    }
}

But as the comments are mentioning, this is pretty inconvenient to use. The reason being is that you can't conveniently resize the arrays. You'd have to recreate them and copy over the elements if you want to make them bigger or smaller.

A better way to go would be like this:

package com.sandbox;


import java.util.ArrayList;
import java.util.List;

public class Sandbox {

    public static void main(String[] args) {
        List<List<Integer>> dt = new ArrayList<>();
        dt.add(new ArrayList<Integer>());
        dt.get(0).add(1);
    }
}

ArrayList, as the name suggests, are backed by an Array. They are (probably, I haven't looked at the source) an abstraction over exactly what you're trying to accomplish. You just don't have to think about the lower level details when you use them.

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

2 Comments

The java compiler returns this error for List<List<Integer>> dt = new ArrayList<>(); -> illegal start of type and points to this character > - Do I need to specify Integer when I create the Arraylist?
@user2415992 You must be using Java 1.6. That's valid in Java 1.7 I believe. Use List<List<Integer>> dt = new ArrayList<List<Integer>>();
1

You could use a: ArrayList<ArrayList<Integer>>?

Then you can simply add an element like:

ArrayList<ArrayList<Integer>> structure = new ArrayList<ArrayList<Integer>>();
structure.add(new ArrayList<Integer>());//add empty array
structure.get(index0).add(value));

How this works:

First one creates an ArrayList who will will store other array lists.

Then we add a second arraylist at index 0 of the first array list.

Graphically one can look at this like

+---+
|   |
| o |
|   |
+---+

with the square being an ArrayList with one element and o being an arrayList with zero elements.

If you state structure.get(0) you access the first element of the large ArrayList, so graphically it will return o. Then I call the add method of that object (o) and I add a value.

So now the structure looks like:

+---------+
|         |
| +-----+ |
| |value| |
| +-----+ |
|         |
+---------+

If I would repeat the command, this would result in:

+---------------+
|               |
| +-----+-----+ |
| |value|value| |
| +-----+-----+ |
|               |
+---------------+

2 Comments

sorry- i'm kind of a noob at this stuff- could you explain exactly how the data is being stored here in the structure.get(index0).add(value); code snippet?- I assume that we access the newly created ArrayList at the index0 index in the structure arraylist, then when we do .add we insert it into the first available index (in this case 0) into that Arraylist at the index0 of structure... sorry for rambling- if there's a concise way you would explain how the memory is being allocated, please just post it here and ignore my comments.
oh... thank you so much! the explanation made a lot of sense!
0

In place of

vertices_passed.get(certain_index).set(index_of_integer_array, Integer.valueof(9))

use

vertices_passed.get(certain_index)[index_of_integer_array] = 9;

if you are using a really old version of Java you need Integer.valueOf(9) or new Integer(9) but not so much for the last several years.

You do have to create the array of Integer and put it into the List before setting it:

int n = ... whatever ... vertices_passed.set(new Integer[n]);

Comments

0

This will work:

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

vertices_passed.get(certain_index).set(index_of_integer_array, 9)

No need to do: Integer.valueof(9), just 9 will do, as Java does an automatic conversion here.

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.