1

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.

4
  • 5
    Given that List extends Collection, the short answer is: you can't Commented Mar 2, 2014 at 13:41
  • 5
    It's nonsense. java.util.List is a collection. Commented Mar 2, 2014 at 13:42
  • Did the interviewer say java.util.List or just a list? Commented Mar 2, 2014 at 14:41
  • @ksuralta java.util.List Commented Mar 2, 2014 at 15:16

3 Answers 3

4

like this using Arrays ....

Integer[] spam = new Integer[] { 1, 2, 3 };
    List<Integer> test=Arrays.asList(spam);
Sign up to request clarification or add additional context in comments.

2 Comments

@Scorpion: That's true. But the OP wants to assign a value to a List from a non List type object.
@Scorpion yes, what's wrong with both answers? please consider the down votes, seem unfair
4

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.

3 Comments

Here we are creating List by using String array. String array is a collection and the question is to make list without collection.
@SSingh a String[] is not a Collection, it's an array. In Java, by collection we mean: a class that implements the Collection interface.
Actually answer what I gave to the interviewer is the Array.asList("one","two") but he didn't agree with my answer and after telling "string array is a collection", he switched to next question.
1

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

Comments

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.