arraylist interview questions
Top arraylist frequently asked interview questions
I've always been one to simply use:
List<String> names = new ArrayList<String>();
I use the interface as the type name for portability, so that when I ask questions such as these I can rework my code.
When should LinkedList
be used over ArrayList
and vice-versa?
Source: (StackOverflow)
I want to create a list of options for testing purposes. At first, I did this:
ArrayList<String> places = new ArrayList<String>();
places.add("Buenos Aires");
places.add("Córdoba");
places.add("La Plata");
Then I refactored the code as follows:
ArrayList<String> places = new ArrayList<String>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
Is there a better way to do this?
Source: (StackOverflow)
What are the differences between these two data structures and where should you use each of them?
Source: (StackOverflow)
I have a ArrayList
l1 of size 10. I assign l1 to new list reference type l2.
Will l1 and l2 point to same arraylist object? Or a copy of arraylist object is assigned to l2.
Because Using l2 reference, if I update the list object, it reflects the changes in l1 reference type also.
eg.
List<Integer> l1 = new ArrayList<Integer>();
for(int i=1;i<=10;i++)
l1.add(i);
List l2 = l1;
l2.clear();
Is there no other way to assign a copy of list object to new reference variable, apart from creating 2 list objects, and doing copy on collections from old to new?
Thanks in advance
Source: (StackOverflow)
public static ArrayList mainList = someList;
How can I get a specific item from this ArrayList
? mainList[3]
?
Source: (StackOverflow)
How can I split an ArrayList (size=1000) in multiple ArrayLists of the same size (=10) ?
ArrayList<Integer> results;
Source: (StackOverflow)
I have an ArrayList
, a Collection class of Java, as follows:
ArrayList<String> animals = new ArrayList<String>();
animals.add("bat");
animals.add("owl");
animals.add("bat");
animals.add("bat");
As you can see, the animals
ArrayList
consists of 3 bat
elements and one owl
element. I was wondering if there is any API in the Collection framework that returns the number of bat
occurrences or if there is another way to determine the number of occurrences.
I found that Google's Collection Multiset
does have an API that returns the total number of occurrences of an element. But that is compatible only with JDK 1.5. Our product is currently in JDK 1.6, so I cannot use it.
Source: (StackOverflow)
This question already has an answer here:
I'm trying to remove some elements from ArrayList
while iterating it like this:
for (String str : myArrayList) {
if (someCondition) {
myArrayList.remove(str);
}
}
Of course, I get ConcurrentModificationException
when trying to remove item from the list at the same time when iterating myArrayList
. Is there some simple solution to do that right?
Source: (StackOverflow)
This question already has an answer here:
i have a class named Fruit i creating a list of this class and add each fruit in each objects i want to sort this list based on the order of name
public class Fruit{
private String fruitName;
private String fruitDesc;
private int quantity;
public String getFruitName() {
return fruitName;
}
public void setFruitName(String fruitName) {
this.fruitName = fruitName;
}
public String getFruitDesc() {
return fruitDesc;
}
public void setFruitDesc(String fruitDesc) {
this.fruitDesc = fruitDesc;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
and i create its list using for loop
List<Fruit> fruits= new ArrayList<Fruit>();
Fruit fruit;
for(int i=0;i<100;i++)
{
fruit = new fruit();
fruit.setname(...);
fruits.add(fruit);
}
and i need to sort this arraylist using the fruit name in each objet in the list
how??
Source: (StackOverflow)
Given Iterator<Element>
, how can we convert that iterator to ArrayList<Element>
(or List<Element>
) in the best and fastest way possible, so that we can use ArrayList
's operations on it such as get(index)
, add(element)
, etc.
Source: (StackOverflow)
So, normally ArrayList.toArray()
would return a type of Object[]
....but supposed it's an
Arraylist
of object Custom
, how do I make toArray()
to return a type of Custom[]
rather than Object[]
?
Source: (StackOverflow)
Right now, I have a program containing a piece of code that looks like this:
while (arrayList.iterator().hasNext()) {
//value is equal to a String value
if( arrayList.iterator().next().equals(value)) {
// do something
}
}
Am I doing that right, as far as iterating through the ArrayList goes?
The error I am getting is:
java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(Unknown Source)
at main1.endElement(main1.java:244)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at main1.traverse(main1.java:73)
at main1.traverse(main1.java:102)
at main1.traverse(main1.java:102)
at main1.main(main1.java:404)
I would show the rest of the code, but it's pretty extensive, and if I am not doing the iteration correctly, I would assume the only possibility is that I am not initializing the ArrayList
properly.
Source: (StackOverflow)
I have two arraylist filelist
and imgList
which related to each other, e.g. "H1.txt" related to "e1.jpg". How to automatically randomized the list of imgList
according to the randomization of fileList
? Like in excel, if we sort certain column, the other column will automatically follow?
String [] file = {"H1.txt","H2.txt","H3.txt","M4.txt","M5.txt","M6.txt"};
ArrayList<String> fileList = new ArrayList<String>(Arrays.asList(file));
String [] img = {"e1.jpg","e2.jpg","e3.jpg","e4.jpg","e5.jpg","e6.jpg"};
ArrayList<String> imgList = new ArrayList<String>(Arrays.asList(img));
//randomized files
Collections.shuffle(fileList);
output after randomization e.g.:
fileList = {"M4.txt","M6.txt","H3.txt","M5.txt","H2.txt","H1.txt"};
intended output:
imgList = {"e4.jpg","e6.jpg","e3.jpg","e5.jpg","e2.jpg","e1.jpg"};
Source: (StackOverflow)