1

Is it possible to do the following?

private static ArrayList<integer<integer<String>>> myArrayList;

In other words, create an ArrayList with declared element syntax?

Example:

myArrayList[0][0] = "This is the string.";

If not, is it possible to do such with normal arrays?

Juvanis
  • 25,802
  • 5
  • 69
  • 87
basickarl
  • 37,187
  • 64
  • 214
  • 335

4 Answers4

1

It seems you are looking for multi-dimensional arrays in Java, declared as follows:

String [][] list = new String[10][10];
list[0][0] = "This is a string";
System.out.println(list[0][0]);
Vikdor
  • 23,934
  • 10
  • 61
  • 84
1

You can declare List of List in the following way: -

List<List<String>> listOfList = new ArrayList<List<String>>();

Initialize your List, and then to add a String to your list element: -

listOfList.add(new ArrayList<String>());

listOfList.get(0).add("my String");
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

You can do

List<List<String>> list = new ArrayList<List<String>>();
list.add(new ArrayList<String>());

list.get(0).add("This is the string");
jlordo
  • 37,490
  • 6
  • 58
  • 83
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

What you would want to achieve in realtime?, Java defines set of APIs, which has its own usage and purpose. If you want to use multidimensional array, I would suggest to go for Object[][], though you should be able to declare

ArrayList list[][] = new ArrayList[5][7];

By doing so, you would be making you operation bit complex. And more over ArrayList is nothing but single dimensional Object array.

FYI

How to create a Multidimensional ArrayList in Java?

Community
  • 1
  • 1
Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46
  • You've got the wrong syntax there unfortunately. Not sure what you mean by "achieve in realtime" either. – Paul Bellora Oct 18 '12 at 02:11
  • I dont understand by the comment 'wrong syntax', It is a valid java statement that can be compiled. – Satheesh Cheveri Oct 18 '12 at 06:17
  • It does compile but I don't think it's doing what you meant. `new ArrayList[5][7]` will instantiate an array of arrays of raw `ArrayList`s, which isn't what the OP wants. You probably meant what Peter and Rohit demonstrate. – Paul Bellora Oct 18 '12 at 06:22
  • That doesn't mean the syntax is wrong, agreed my answer is not that relevant OP's question. – Satheesh Cheveri Oct 18 '12 at 08:13