Is there a simple way to create a 2-d collection?
A two-dimensional collection is essentially having lists within a list. For example, to create a 2D ArrayList of strings, you would do something like this:
ArrayList<ArrayList<String>> stringList = new ArrayList<ArrayList<String>>();
To add a new row, you would simply add a new ArrayList:
stringList.add(new ArrayList<String>());
And here's how to add an element to the first row:
stringList.get(0).add("example string");
A 2d collection is a bit abstract... what do you mean?
A double entry array is a 2d collection.
Why don't you use Multimaps from Guava library?
https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained
,
Those collections ads everything that the jdk is missing for dealing with 2d collections.
Related
I am trying to implement Bucket Sort in Java without using Collections framework, on my own. I have a problem in implementing it.
I wanted to store a list of elements in a particular array index.
For Ex:
arr[0]={1,2,3,4}; //Here Array index 0 will be storing 4 values.
So I chose to have a linked list to store those values and then to map the array index with that linked list.
But I am not aware of how to map an Array index to a Linked List.
For Ex:
arr[0]->LinkedList1
arr[2]->LinkedList2
// ... and so on
Please suggest how to implement it.
In Java, arrays or Collections are simply a collection of objects of the same type. So, for your requirement, what you need is an array of lists.
List[] arrayOfLists = {};
This creates an array whose each member is a list (You can also create an array of LinkedList if you like).
Now, create a LinkedList and assign it to index 0 of the array.
LinkedList list1 = new LinkedList();
arrayOfLists[0] = list1;
Hope it helps.
Can anyone please explain in which situation are we supposed to use ArrayList instead of a simple array and what is the difference between these two and how to initialize the ArrayList.
I am new to java so use example if possible.
Whenever you don't know the size to be created, you can use ArrayList.
If you just want speed access, you can use array (indexing in array is faster than ArrayList).
And also, ArrayList reduces the complexity of coding.
ArrayList can be initialized like this
ArrayList al = new ArrayList();
Add values to the ArrayList
al.add("String");
al.add(number);
I have two arrayList that can contain 0 or many int and I need to create a new arrayList from these two with values that are in both. In this case [4,23]
Example
arrayList1 [3 ,4 ,7,23,12]
arrayList2 [13,4,17,23,15]
In this example I need a new arrayList containing [4,23]
Is there an easy way to do it with .contains() or some similar method. Or should I
loop through both list and check for equality and create the new list ?
Knowing that one or the other arrayList can be empty.
Thank you
This would be more efficient with sets, but even just with lists, it's simple to use retainAll:
// Start by copying arrayList1
List<Integer> result = new ArrayList<Integer>(arrayList1);
result.retainAll(arrayList2);
That's assuming you don't want to modify arrayList1. If you don't mind about that, you can skip the first step:
arrayList1.retainAll(arrayList2);
You should strongly consider using sets if that's what you're logically talking about though.
You can simply use retainAll:
list1.retainAll(list2);
Or if you don't want to modify list1:
List<Integer> intersection = new ArrayList<>(list1);
intersection.retainAll(list2);
Can you give any reasonable example for a ArrayList<ArrayList<E>>, such as declaring, initializing, adding elements and iterating them. Is this one of the way to get 2-dimensional Array behavior in Java?
Yes, an ArrayList<ArrayList<E>> is similar to a two-dimensional array of E (E[][]). It has all the common differences between using a List and using arrays in Java (List is a higher-level API, supports resizing, adding elements at arbitrary positions, ...).
You don't treat it any different from a normal List, except that the elements it contains are actually other List objects:
Initialize it:
ArrayList<ArrayList<E>> listOfLists = new ArrayList<ArrayList<E>>();
Iterate over it:
for (ArrayList<E> innerList : listOfLists) {
doSomethingWithInnerList(innerList);
}
Add to it:
ArrayList<E> newInnerList = new ArrayList<E>();
// add stuff to newInnerList
listOfLists.add(newInnerList);
The only thing I want to add to Joachim Sauer's answer is that yes, an ArrayList<ArrayList<E>> can be similar to a two-dimensional array of E (E[][]) with one additional twist (in addition to all the usual differences between one-dimensional arrays and lists):
Using a list of lists, you can make the equivalent of a "jagged" array. Not all of the inner lists need to have the same size(), whereas in a two-dimensional array, all of the "rows" of E[][] by definition have identical lengths. It's "rectangular". A list of lists doesn't have to be rectangular; it can be jagged.
ArrayList is used to hold array of objects. On the other it can have duplicate values. when you need a fast insertion/deletion you can use it. it holds the values in the same order as it is entered into it. For example
List<String> ls= new ArrayList<String>();
ls.add("foo");
ls.add("bar");
for(String val:ls){
System.out.println("Value :" + val);
}
Can I add a group of ArrayList into a single ArrayList?
The ArrayList groups are of different sizes. Then How can I recognize each ArrayList?
I hope I understand your question correctly.
An ArrayList is a list of Object types. If you wish to add an ArrayList as the item in your ArrayList, then you can do this.
An example
ArrayList list = new ArrayList();
ArrayList list2 = new ArrayList();
ArrayList list3 = new ArrayList();
list.add(list2);
list.add(list3);
This will result in your first ArrayList (list) containing two arraylists in position 0 and 1.
If however you are looking to add the contents of several ArrayLists to a single list of elements, then you use addAll on the ArrayList. Such as
ArrayList consolidatedList = new ArrayList();
list.addAll(list2);
list.addAll(list3);
I didn't actually get what you want to do. Do you want to add all elements from different ArrayLists to one resulting ArrayList? If so, use ArrayList#addAll(Collection collection) method.
If you want an ArrayList of ArrayLists, use ArrayList#add(Object o) method. You can also parameterize your resulting ArrayList like this:
List<ArrayList> res = new ArrayList<ArrayList>()
Yes you can add a "group of ArrayLists" into a single array list. If you want to "recognize" your ArrayLists, I would recommend using a Map, a HashMap for example.
Please also think about accepting answers to your other questions, and post code in your questions, you will get better help and people will hate you less.