I am trying to find out a java interview answer to the question interviewer asked me was:
Question is: How can we make a java.util.List without using collection?
Any help would be greatly appreciated.
like this using Arrays ....
Integer[] spam = new Integer[] { 1, 2, 3 };
List<Integer> test=Arrays.asList(spam);
List from a non List type object.Maybe the interviewer meant this?
List<String> aList = Arrays.asList("a", "b", "c");
In the above snippet we're creating a list from an array (under the hood asList() converts the varargs into a T[], with T being the generic type of the passed arguments). But the List interface extends from the Collection interface, so what's the point? a List is a Collection anyway.
String[] is not a Collection, it's an array. In Java, by collection we mean: a class that implements the Collection interface.Create your own List class and inside that create a private array with some initial size. and implement the similar methods of collection list in your class to access the array object definitely you will required few flags to check the current state of array. When your array reached to filled than create another array with higher size and copy older object of array inside new array. You can get the examples on net. check this link http://web.eecs.umich.edu/~aprakash/eecs282/lectures/10-arraylists.pdf
ListextendsCollection, the short answer is: you can'tjava.util.Listis a collection.java.util.Listor just a list?