Connecting Array Index with Linked List - java

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.

Related

After using Arrays.sort() how do I reverse array to its initial position?

I had to sort array to find its Median, but now I need to recover the initial value of array, put it as it was. Is that possible?
You can't do that. Sorting is irreversible. However, you could also add all elements of your original list to a new ArrayList and then sort that list.
List<String> original = ...;
List<String> copy = new ArrayList<>(original);
Collections.sort(copy);
I would not worry about the footprint of the copy. It is a shallow copy, that means that the elements themselves are not copied, but only a new list is created with references to the elements contained in the original list. And that operation is quite fast. I would only worry if the list was very, very big.
Using #McEmperors answer but with arrays
Object[] saved = Arrays.copyOf(old, old.length);
Array.sort(old);
There are several ways to achieve what you want:
copy the array before sorting
int[] unsorted = {2,3,1};
int[] sorted = unsorted.clone();
Arrays.sort(sorted);
//find mean in sorted then proceed with unsorted
create a custom sort function that retains a mapping between the positions.
find the mean without sorting

initializing a multidimensional list with n empty 1D lists in Java

I have a multi-dimensional List
List<List<Integer>> myList;
I want it its dimension to be specified at run time, so in the code, I put:
myList = Collections.synchronizedList(new ArrayList<List<Integer>>(n));
I was hoping this would initialize mylist as a list with n elements each having zero elements but that didn't happen. I only get an empty list. apparently that constructor, "Constructs an empty list with the specified initial capacity." which is not quite what I want.
I understand that I can loop over mylist and add() empty one-dimensional lists, but is there any way of achieving what I want with less codes of code?
First of all the constructor ArrayList<T>(int capacity) does not insert any element in the list, so you are not specifying a size but an initial capacity.
Basically you allow the list to insert up to n elements without the need of internal resizing.
So the outer list is still empty. You can't use Collections.fill because you need a different internal List<Integer> every time, and fill would just set all elements to the same reference. So you are forced to insert them manually:
for (int i = 0; i < n; ++i)
myList.add(new ArrayList<Integer>());
Mind that in any case, since you specified it as a List<List<Integer>> (which makes sense), Java wouldn't be able to default initialize anything, since List<Integer> in just an interface.

Adding a element of Generic type <T> to an arrayList of LinkedList's

I want to make a hash table class in java where I store the key,value pairs in an ArrayList of Linked List's. I do this by declaring
ArrayList<LinkedList<T>> storage = new ArrayList();
I then want to create a linkList object that I can use to then create a new linked list inside of each index of the arrayList. I do this by declaring:
LinkedList<T> list = new LinkedList<T>();
Then I have my add function set up to add elements to the first index of the LinkedList that is inside the Hashed key index of the arrayList as such:
public void add(K key, T value){
int arrayListIndex = (key.hashCode()) % this.initialCapacity;
System.out.println(arrayListIndex); //This tells us where we access the Array List;
if (hashBrown.get(arrayListIndex) == null){
hashBrown.add(arrayListIndex, list);
hashBrown.get(arrayListIndex).addFirst(value);
}
}
Everytime I run this code I receive an error where my index is 7 and my size is 0. This causes an error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:571)
at java.util.ArrayList.get(ArrayList.java:349)
at FastHashtable.add(FastHashtable.java:72)
at FastHashtable.main(FastHashtable.java:145)
I am unable to track down where this index out of bounds error is coming from can anyone offer advice. I am fairly new at working with ArrayLists which makes me think my original declaration of the arrayList is incorrect.
You are confusing an ArrayLists capacity with its size. From the Oracle Java documentation:
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
Instead, you should be looking at creating a plain array (e.g. Object[] a = new Object[maxSize]) in which you can actually assign objects (linked lists in this case) at arbitrary index values. If you only want to store linked lists, create a LinkedList<T>[] array.
You cannot add to a list at index 7 if there are 0 elements in it. You can only add at the end (with the add method without index) or at a position that is no larger than the current list's size().
When the list is empty and you add something at index 7, what do you expect the list to contain at the first position then, and what at index 6? (I once created a subclass of list to fill everything up to the index with null when the addition index is larger than the list size, but this behaviour is not universal enough to be part of List's semantics.
[edit in response to comment here and to praseodym's answer]
You could simply fill the (array) list with nulls replace those with a linked list when the respective position is first accessed. (Be sure to use set and not add, though, which is probably what you want above as well.)
Alternatively, you could create an array of the desired size (that will be full of nulls by default) and "wrap" it into a (non-resizable) list via Arrays.asList. You have to ignore an "unchecked conversion" warning then, however (not that you could avoid warnings when using an array). Also, I suggest you read "Program to an interface, not an implementation".

Java generic collections

I start learning the Java generic collection using Deitel Harvey book - but I am facing a difficulty understanding the three line of codes below - Do all of them perform the same operation on by intializing and adding the relevant values of array ( colors ) to the LinkList variable (list1). How does the second method and third method works - I am having a bit difficulty understanding how Arrays can viewed as a list.. As I know arrays are not dynamic data structure, they have fixed sized length, adding/ removing elements on array can not be done on running time comparing to Lists in general.
String[] colors = { "black", "white", "blue", "cyan" };
List< String > list1 = new LinkedList< String >();
// method 1 of initalizing and adding elments to the list
for (String color : colors)
list1.add(color);
// method 2 of initializing and adding elements to the list
List< String > list1 = new LinkedList< String > (Arrays.asList(colors));
// method 3 of initializing and adding elements to the list
List< String > list1 = Arrays.asList(colors);
Please help me understand my queries above, don't judge me as I am still new to this.
Thank you, Sinan
Actually knowledge of generics is not necessary for answering this question.
As you correctly identifier arrays are static in the sense that you can't add elements to them or remove them.
Lists, however, usually allow those operations.
The List returned by Arrays.asList() does have the add/remove methods (otherwise it would not be a valid List). However actually calling those methods will throw an UnsupportedOperationException exactly because you can't actually add elements to an array (for which this List is simply a view/wrapper).
Operations that don't structurally modify the list (i.e. that don't change the number of elements in the list) are entirely possible: set(int, E) works just fine on the List returned by Arrays.asList().
Arrays.asList returns a fixed-size list backed by the specified array.
It is actually a bridge between Array and Collection framework. But returned list write through to the array.
Only your first method does anything to the LinkedList you have initially assigned into list1. The other two assign a new, unrelated list to it. The third option assigns something that isn't a LinkedList, but a special implementation of the List interface backed by your String array. In the third case you won't be able to add/remove elements from the list, but you can iterate over it and update existing slots. Basically, it does what a plain array does, just through the List interface.
Arrays.asList creates a List from an Array. Arrays in general can't be viewed as lists in Java. They can only be wrapped in a list.
So method 2 is used to have a specific list implementation LinkedList in this case.
to Method 2, just check the Api here:
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/LinkedList.html#LinkedList(java.util.Collection)
For sure, Lists implement the Collections Interface so this Constructor will work here.
to Method 3, just check out the Api here: http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList(T...)
Every time you are interested in implementation you can look into certain method. For example, by press Ctrl+left mouse button onto method or class.
// method 2 of initializing and adding elements to the list
List<String> list1 = new LinkedList<String> (Arrays.asList(colors));
This code leads to:
List<String> list1 = new LinkedList<String> (new ArrayList<String>(colors));
In constructor of ArrayList:
ArrayList(E[] array) {
if (array==null)
throw new NullPointerException();
a = array;
}
the actual array is copied to encapsulated private array field(link is copied).
Then in constructor of LinkedList:
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
Every element of passed collection is added to the LinkedList.
if you see the link below
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/LinkedList.html#LinkedList%28java.util.Collection%29
you will see the constructor of linked list class which is accepting a collection object as parameter.
Any in your post, the 2nd and 3 rd lines are passing an object of collection class(i.e Arrays.asList is finally giving a List which is a sub class of collection).
So both 2nd and 3rd lines fairly valid implementations.
More over you can observe one more good coding practice in all the 3 lines.
That is
writing code to interceptors than to classes
. (referring
LinkedList
instance with
List
interface)
Always try to refer your classes with interceptors which is a good practice

What is the use of ArrayList<ArrayList<E>>

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

Categories

Resources