-2

Can someone please tell me what I'm doing wrong, cause I have this error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at javaapplication4.JavaApplication4.main(JavaApplication4.java:41)

    ArrayList<List<Cartas>> tabuleiro = new ArrayList<>();

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

    pos.add(40);pos.add(50);pos.add(60);pos.add(31);pos.add(41);pos.add(22);pos.add(32);pos.add(42);pos.add(13);pos.add(23);pos.add(33);pos.add(24);pos.add(34);

        for(int x=0; x<9; x++){
            for(int y=0; y<7; y++){
                for (Integer p : pos) {
                    if (p == (x*10+y)) {
                        tabuleiro.get(x).set(y, new Cartas(x,y,false));
                    }
                }
            }
        }

        for(int x=0; x<9; x++){
            for(int y=0; y<7; y++){
                System.out.println(tabuleiro.get(x).get(y).nomeCV);
            }
        }
eddie
  • 1,252
  • 3
  • 15
  • 20
Ricardo Origin
  • 175
  • 3
  • 14
  • The answer depends on how you want to insert elements into it, and how you want to read them. Then we can help you decide how to implement it. – Joffrey May 05 '15 at 23:11
  • 2
    This question is rather trivial. Look up documentation/tutorials on working with arrays in Java. Concentrate on working with two-dimensional arrays. Although once you know how to work with one-dimensional arrays, you're pretty much done. 2D arrays work just like a matrix. `arr[i][j]` is essentially the value at row `i`, column `j`. Knowing this, think about how you would assign values to locations in your 2D array to make the pattern you describe. – Vivin Paliath May 05 '15 at 23:11
  • 1
    @VivinPaliath The OP said "*i just want an idea of how can i create a bidemensional arraylist*". You're talking about arrays, not `ArrayList`. – user1803551 May 05 '15 at 23:20
  • Possible duplicate of [Two Dimensional ArrayList](http://stackoverflow.com/questions/15285179/two-dimensional-arraylist). See also [this](http://stackoverflow.com/questions/10768170/how-do-i-declare-a-2d-string-arraylist) and [this](http://stackoverflow.com/questions/10866205/2-dimensional-array-list). – user1803551 May 05 '15 at 23:22
  • @user1803551 The first line of OP's question reads "I need to create bidemensional [sic] *array*", which is why I mentioned arrays. I see later that he talks about array-lists. I didn't catch that initially. – Vivin Paliath May 05 '15 at 23:56
  • 2
    An alternative is using a `HashMap` and define your own `IntPair` class that has two `int` values. – M. Shaw May 06 '15 at 00:05
  • Why did you completely change your question? This is not how SO works, the goal is for other users to have the answers too. If you have another question as you progess, please open another post. – Joffrey May 14 '15 at 09:25

4 Answers4

1

To create an arraylist within an arraylist:

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

(Note, list doesn't have anything inside it at this point)Then, you create a new ArrayList inside list like this:

list.add(new ArrayList<String>);

Then:

list.get(0).add("Hello);

To add a String to the ArrayList at list's 0th index

Dan12-16
  • 351
  • 1
  • 8
1

I'm not sure I understand your question, as you seem to answer yourself. The answer depends on how you want to insert elements into it, and how you want to read them.

Here are some ways I can think of so far, which I can't choose from without more information:

  • use a bidimensional array C[][], which may contain null values. This solution requires to know the size of the array from the start.

  • use an ArrayList<ArrayList<C>>, where the root list is the list of lines, and each line may contain null items and C objects. You may add more lines as needed, and more elements to a line as needed.

  • use 2 ArrayList<List<C>>, where the first is the list of lines, and the second is the list of columns. The lines and columns would share the same objects, but don't need to contain any null values at all: each line is just a list of the non null objects contained in the line, and the same goes for each column.

    This last solution is less easy to manipulate, but is much more memory efficient if your matrix is really sparse (contains few objects but a lot of lines and columns).

Community
  • 1
  • 1
Joffrey
  • 32,348
  • 6
  • 68
  • 100
1

To create an 2D ArrayList containing "C" objects then you can instantiate a 2D list such as:

C obj = new C(); //Creates a "C" object
ArrayList<ArrayList<C>> myArray = new ArrayList<ArrayList<C>>(); //Creates a 2 Dimensional ArrayList
myArray.add(new ArrayList<C>); //Adds an ArrayList<C> to the the 2D ArrayList
myArray.get(0).add(obj); //Adds a "C" object to the ArrayList that was added on the previous line

For better understanding of ArrayLists refer to this link

Rezwan Azfar Haleem
  • 1,198
  • 13
  • 23
1

You could use Guava Table

Table<Integer, Integer, YourClassC> table = HashBasedTable.create();
table.put(0, 8, new YourClassC());
table.put(1, 3, new YourClassC());
table.put(2, 2, new YourClassC());
...

table.get(0, 0); // returns null
table.get(0, 8); // returns first instance of YourClassC
Arnaud
  • 36
  • 5