How to create a 2D ArrayList? - java

I'm trying to use Java to create a 2-dimensional array. The size of rows is known, while the size of columns is unknown. Here is my code and it doesn't work. Could anyone give me some idea?
ArrayList<Integer> paths[];
paths = new ArrayList[2];// 2 paths
for (int i=0; i<2; ++i)
paths[i].add(1); // add an element to each path

Initialize the array element before adding to it. Put the initialization into the for loop:
#SuppressWarnings("unchecked")
ArrayList<Integer>[] paths = new ArrayList[2];
for (int i=0; i<2; ++i) {
paths[i] = new ArrayList<Integer>();
paths[i].add(1);
}
This way you can avoid the NullPointerException.

I would recomend this
ArrayList<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
for (int i=0; i<2; ++i)
paths.add(new ArrayList<Integer>());

This is a "2d" ArrayList:
ArrayList<ArrayList<Integer>> paths = new ArrayList<>();
And here is the non-diamond operator version for Java < 1.7:
ArrayList<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();

Related

Is there any option to create few ArrayLists automaticly with loop?

I have project to create storage, and i am using ArrayLists for every room.
Is there any solution to create more than one ArrayList in loop ?
I just want to reduce amount of code.
Quotes/brackets and other stuff arent working. Is there any solution for beginner?
I was trying something like this.
for(int i=0; i<10; i++}{
ArrayLists list[i] = new ArrayLists();
}
ArrayList list0 = new ArrayList();
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
//up to 10
use arrays.fill and get rid off the loop
ArrayList<Integer>[] al = new ArrayList[5];
Arrays.fill(al, new ArrayList<Integer>());
I am not entirely sure what you want to achieve, but you can have a list of lists like so:
ArrayList<ArrayList<>> listOfLists = new ArrayList<>();
for (int i = 0; i < 10; i++) {
listOfLists.add(new ArrayList<String>());
}
List<List<Integer>> lists = new ArrayList<List<Integer>>();
for (int i = 0; i < 5; i++) {
List<Integer> list = new ArrayList<>();
lists.add(list);
}
Will create Multiple List inside a List.

Creating x ArrayLists

I use a variable class which stores an integer. If the integer is 13, I need 13 arraylists to be created. Any idea as to how I would do this?
This is what is in my while loop:
ArrayList<Integer[]> list + count = new ArrayList<>();
With the following:
ArrayList<ArrayList<Integer>> myLists = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < 4; i++) {
ArrayList<Integer> newList = new ArrayList<Integer>();
myLists.add(newList);
}
Let's say I need the integer 9 to go into the 3rd array list and the integer 3 to go to the 2nd array list. How would i do that?
What are you trying to achieve?
You can create an ArrayList of ArrayLists. So something like:
ArrayList<ArrayList<Integer>> myLists = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < numOfLists; i++) {
ArrayList<Integer> newList = new ArrayList<Integer>();
myLists.add(newList);
}
That way you'll have myLists with all the lists you created.
By concatinating the variable to make another variable name is not possible. The compiler should already told you about this. You can accomplish this by using another ArrayList
List<List<Integer[]>> mainList = new ArrayList<List<Integer[]>>();
for(int i = 0; i < 13; i++){
mainList.add(new ArrayList<Integer[]>());
}
If you want to identify each of the ArrayList by a name you could use a Map.
Map<String, ArrayList<Integer[]>> listsMap = new HashMap<>();
for(int i = 0; i < 13; i++){
listsMap.add("list" + i, new ArrayList<>());
}
Then, if you need to add an integer to a particular list you could do this:
listsMap.get("list3").add(new Integer[]{3});

dynamicaly create arrayList in JAVA

I need to create 50 arraylists but rather than having to initialise them all individually. Is there a way in JAVA that I can do this dynamically??
i.e.
pseudo code:
for int i=1; i<51; i++
List<String> Counti = new ArrayList<String>();
So that in the loop it goes through and create 50 arraylists all called Count1, Count2, Count3 etc up until Count50.
I've tried creating a string and then naming the list by the string but it doesnt seem to recognise that teh name is a variable.
e.g.
for int i=1; i<51; i++
String Name="Count "+i
List<String> Name = new ArrayList<String>();
Instead it just creates a list called "Name"
You can do this with reflection, but this is a pretty bad idea. What you probably want to do is create an arraylist of arraylists.
ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();
for (int i = 0; i < 50; i++)
listOfLists.add(new ArrayList<String>());
You should store them in a List and not create 50 variables
List<List<String>> lists = new ArrayList<ArrayList<String>>();
for (int i = 0; i < 50; i++)
{
lists.add(new ArrayList<String>());
}
You need a List of ArrayList.
List<List<String>> lists = new ArrayList<>();
for (int i = 0; i < 50; i++){
lists.add(new ArrayList<String>());
}
one another possible solution is to use a HashMap like this to name the ArrayLists:
Map<String, ArrayList<String>> mAllArrayListsByName = new HashMap<String, ArrayList<String>>();
for (int i = 0 ; i < 50 ; i++){
mAllArrayListsByName.put("List"+String.valueOf(i), new ArrayList<String>());
}
ArrayList<String> lMyArrayList = mAllArrayListsByName.get("List0"); // get List 0

array of arraylist type error

void radix(int[] arr, int maxDigit){
int exp = 1;
for (int i=0; i<maxDigit; i++) {
ArrayList[] bucket = new ArrayList[10];
for (int n=0; n<10; n++){
bucket[n] = new ArrayList<Integer>();
}
for (int j=0; j<arr.length; j++){
bucket[(arr[j]%exp)%10].add(arr[j]);
}
int ind=0;
for (int k=0; k<10; k++){
for (int n : bucket[k]){
arr[ind] = n;
ind++;
}
}
exp*=10;
}
}
above is my attempt for a radix sort in java.
I keep having an error at this for loop:
for (int n : bucket[k])
Saying that Object can't be converted to int.
However, I declared the array to have an arrayList as its element,
so bucket[k] should be an arraylist.
Can anyone please explain why this happens and how I could get around this problem?
I would appreciate any help. Thanks
You have an ArrayList<Integer>. Try using for(Integer n : bucket[k])
You also need to provide a type for ArrayList. Try ArrayList<Integer>[] bucket = new ArrayList[10];
ArrayList[] bucket = new ArrayList[10];
This does not have the type parameter.
And you should avoid using inflexible, low-level constructs like arrays anyways.
Try to actually express your intents in your code with identifiers and to use first-class objects for all of your key concepts.
You have an ArrayList not ArrayList.
Change:
for(int n : bucket[k])
To:
for(Integer n : bucket[k])
You also need to type your ArrayList declaration with:
ArrayList<Integer>[] bucket = new ArrayList<Integer>[];

Array of arraylists

im having trouble making an array of arraylists, heres's the code : 'ArrayList<Integer>[] solucao= new ArrayList[6];'
and using this code below:
solucao[0].add(1);
solucao[0].size();
solucao[1].size();
solucao[1].add(1);
solucao[2].size();
solucao[2].add(1);
solucao[3].size();
solucao[3].add(1);
solucao[4].size();
solucao[4].add(1);
solucao[5].size();
solucao[5].add(1);
solucao[6].size();
solucao[6].add(1);
solucao[7].size();
solucao[7].add(1);
all the calls for size return null. Anyone knows how to solve it?
Im looking for a data structure of array of arraylists, as each array[i] position will return me an arraylist of integers.
thank you
You have to initialize each ArrayList in the array.
ArrayList[] solucao = new ArrayList[6];
for (int i = 0; i < solucao.length; i++)
solucao[i] = new ArrayList();
I actually thought you couldn't have an array of ArrayList. Apparently you can, but it must be non-generic. You should probably reconsider why you're doing this...
Arrays are just pointers or references. For each for them you have to create a new ArrayList object and store your data in it.
List[] solucao= new ArrayList[5];
for(int i=0;i<solucao.length;i++)
{
solucao[i] = new ArrayList();
solucao[i].add(yourObject);
}
ArrayList<Integer>[] solucao= new ArrayList[6];
Should be new ArrayList<Integer>[6]
Note an IDE would give you a warning about this. Next, initialize each element of the array (Java 7):
for(int i = 0; i < solucao.length; i++) {
solucao[i] = new ArrayList<>();
For store data first you have to create object.
ArrayList<Integer>[] ls = new ArrayList[7];
for (int i = 0; i < ls.length; i++) {
ls[i] = new ArrayList<Integer>();
for(int j = 0 ; j<i ;j++){
ls[i].add(j);
}
System.out.println(ls[i].size());
}

Categories