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?

4
  • Well have you tried it ? what results did you get ? and Integer will work much better than integer :) Commented Oct 17, 2012 at 16:25
  • Do you need a multi dimensional array of strings ? Commented Oct 17, 2012 at 16:26
  • integer should be Integer and also you cannot do Integer<String>. that's not allowed. Commented Oct 17, 2012 at 16:27
  • Is integer a user defined class? If not, your code won't even compile. Are you learning to program without writing programs? Commented Oct 17, 2012 at 19:01

4 Answers 4

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]);
Sign up to request clarification or add additional context in comments.

Comments

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");

Comments

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");

Comments

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?

4 Comments

You've got the wrong syntax there unfortunately. Not sure what you mean by "achieve in realtime" either.
I dont understand by the comment 'wrong syntax', It is a valid java statement that can be compiled.
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 ArrayLists, which isn't what the OP wants. You probably meant what Peter and Rohit demonstrate.
That doesn't mean the syntax is wrong, agreed my answer is not that relevant OP's question.

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.