All Questions
2,419
questions
756
votes
11
answers
1.4m
views
Convert list to array in Java [duplicate]
How can I convert a List to an Array in Java?
Check the code below:
ArrayList<Tienda> tiendas;
List<Tienda> tiendasList;
tiendas = new ArrayList<Tienda>();
Resources res = this....
621
votes
17
answers
646k
views
Why do I get an UnsupportedOperationException when trying to remove an element from a List?
I have this code:
public static String SelectRandomFromTemplate(String template,int count) {
String[] split = template.split("|");
List<String> list=Arrays.asList(split);
Random r = ...
581
votes
40
answers
1.0m
views
How do I remove repeated elements from ArrayList?
I have an ArrayList<String>, and I want to remove repeated strings from it. How can I do this?
500
votes
12
answers
478k
views
ArrayList vs List<> in C#
What is the difference between ArrayList and List<> in C#?
Is it only that List<> has a type while ArrayList doesn't?
464
votes
21
answers
1.3m
views
How to sort a List/ArrayList?
I have a List of doubles in java and I want to sort ArrayList in descending order.
Input ArrayList is as below:
List<Double> testList = new ArrayList();
testList.add(0.5);
testList.add(0.2);
...
400
votes
10
answers
497k
views
How to avoid "ConcurrentModificationException" while removing elements from `ArrayList` while iterating it? [duplicate]
I'm trying to remove some elements from an ArrayList while iterating it like this:
for (String str : myArrayList) {
if (someCondition) {
myArrayList.remove(str);
}
}
Of course, I get ...
299
votes
9
answers
203k
views
Java List.add() UnsupportedOperationException
I try to add objects to a List<String> instance but it throws an UnsupportedOperationException.
Does anyone know why?
My Java code:
String[] membersArray = request.getParameterValues('members')...
268
votes
9
answers
203k
views
How to randomize two ArrayLists in the same fashion?
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?...
260
votes
16
answers
268k
views
When to use a linked list over an array/array list?
I use a lot of lists and arrays but I have yet to come across a scenario in which the array list couldn't be used just as easily as, if not easier than, the linked list. I was hoping someone could ...
211
votes
9
answers
543k
views
How to split() a delimited string to a List<String>
I had this code:
String[] lineElements;
. . .
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((...
168
votes
1
answer
301k
views
What is the difference between List and ArrayList? [duplicate]
I've been using ArrayList recently in my android project at the office and I'm a bit confused between List and ArrayList, what is the difference of the two and what should I use?
Also I saw some ...
116
votes
1
answer
36k
views
Why is Java's AbstractList's removeRange() method protected?
Does anyone have any idea, why removeRange method in AbstractList (and also in ArrayList) is protected? It looks like a quite well-defined and useful operation, but still, to use it, we're forced to ...
110
votes
9
answers
275k
views
List<String> to ArrayList<String> conversion issue
I have a following method...which actually takes the list of sentences and splits each sentence into words. Here is it:
public List<String> getWords(List<String> strSentences){
allWords = ...
102
votes
4
answers
127k
views
Convert an array into an ArrayList [duplicate]
I'm having a lot of trouble turning an array into an ArrayList in Java. This is my array right now:
Card[] hand = new Card[2];
"hand" holds an array of "Cards". How this would look like as an ...
101
votes
20
answers
242k
views
Option to ignore case with .contains method?
Is there an option to ignore case with .contains() method?
I have an ArrayList of DVD object. Each DVD object has a few elements, one of them is a title. And I have a method that searches for a ...
99
votes
23
answers
121k
views
In Java how do you sort one list based on another?
I've seen several other questions similiar to this one but I haven't really been able to find anything that resolves my problem.
My use case is this: user has a list of items initially (listA). They ...
82
votes
14
answers
196k
views
Convert List<String> to List<Integer> directly
After parsing my file " s" contains AttributeGet:1,16,10106,10111
So I need to get all the numbers after colon in the attributeIDGet List. I know there are several ways to do it. But is there any ...
78
votes
6
answers
212k
views
How to remove the last element added into the List?
I have a List in c# in which i am adding list fields.Now while adding i have to check condition,if the condition satisfies then i need to remove the last row added from the list.
Here is my sample ...
77
votes
10
answers
112k
views
find out the elements of an arraylist which is not present in another arraylist
I have to find a best way to find out that elements which is not presented in the second arraylist.
suppose
Arraylist a,b,
Arraylist a={1,2,3,4,5};
Arraylist b={2,3,4};
So basically what I want ...
69
votes
8
answers
175k
views
How does one convert a HashMap to a List in Java?
In Java, how does one get the values of a HashMap returned as a List?
68
votes
4
answers
64k
views
How to convert an ArrayList to a strongly typed generic list without using a foreach?
See the code sample below. I need the ArrayList to be a generic List. I don't want to use foreach.
ArrayList arrayList = GetArrayListOfInts();
List<int> intList = new List<int>();
/...
68
votes
4
answers
224k
views
Check if a String is in an ArrayList of Strings
How can I check if a String is there in the List?
I want to assign 1 to temp if there is a result, 2 otherwise.
My current code is:
Integer temp = 0;
List<String> bankAccNos = new ArrayList&...
56
votes
2
answers
159k
views
Dynamically adding elements to ArrayList in Groovy
I am new to Groovy and, despite reading many articles and questions about this, I am still not clear of what is going on.
From what I understood so far, when you create a new array in Groovy, the ...
55
votes
15
answers
260k
views
Java Compare Two List's object values?
I have two list **ListA<MyData> listA = new ArrayList<MyData>()** and ListB<MyData> listB = new ArrayList<MyData>() both contain object of type MyData and MyData contain these ...
54
votes
2
answers
32k
views
UnsupportedOperationException in AbstractList.remove() when operating on ArrayList
ArrayList's list iterator does implement the remove method, however, I get the following exception thrown:
UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:144)
By ...
54
votes
6
answers
30k
views
ArrayList vs List<object>
I saw this reply from Jon on Initialize generic object with unknown type:
If you want a single collection to
contain multiple unrelated types of
values, however, you will have to use
List<...
52
votes
7
answers
58k
views
Incompatible types List of List and ArrayList of ArrayList
The below line gives me error :
Incompatible Types.
List<List<Integer>> output = new ArrayList<ArrayList<Integer>>();
What is the reason?
EDIT
I understand if I change my ...
47
votes
8
answers
28k
views
c# When should I use List and when should I use arraylist?
As the title says when should I use List and when should I use ArrayList?
Thanks
46
votes
1
answer
102k
views
What is the difference between an Array, ArrayList and a List? [duplicate]
I am wondering what the exact difference is between a Array, ArrayList and a List (as they all have similar concepts) and where you would use one over the other.
Example:
Array
For the Array we can ...
41
votes
10
answers
232k
views
Simple way to compare 2 ArrayLists
I have 2 arraylists of string object.
List<String> sourceList = new ArrayList<String>();
List<String> destinationList = new ArrayList<String>();
I have some logic where i ...
37
votes
5
answers
43k
views
ArrayList vs LinkedList from memory allocation perspective
I need to store a large amount of information, say for example 'names' in a java List. The number of items can change (or in short I cannot predefine the size). I am of the opinion that from a memory ...
35
votes
7
answers
6k
views
Why is an ArrayList of ArrayLists not multidimensional?
I recently appeared for an interview in which the interviewer asked me a question regarding Arrays and ArrayList.
He asked me if an array of arrays can be multidimensional, then why is an ArrayList ...
34
votes
7
answers
114k
views
Java ArrayList clear() function
Let's say I have a ArrayList<String> data;
I'm adding some data into this Array, using data.add() function.
Let's say I've added 10 Strings into this array. The size of the array is 10 now.
...
34
votes
6
answers
28k
views
Remove duplicates from List using Guava
How can we remove duplicates from List with the help of Guava api?
Currently I am following this:
private List<T> removeDuplicate(List<T> list){
return new ArrayList<T>(new ...
31
votes
7
answers
47k
views
Reason for - List list = new ArrayList(); [duplicate]
I have seen code like this many times:
List<String> list = new ArrayList<String>();
Why do people take the parent of ArrayList (and other classes) instead of the type of the generated ...
31
votes
3
answers
46k
views
Merge 3 arraylist to one
I want to merge down 3 arraylist in one in java. Does anyone know which is the best way to do such a thing?
30
votes
6
answers
64k
views
Finding out if a list of Objects contains something with a specified field value?
I have a list of DTO received from a DB, and they have an ID. I want to ensure that my list contains an object with a specified ID. Apparently creating an object with expected fields in this case won'...
30
votes
6
answers
57k
views
Why can't you have a "List<List<String>>" in Java? [duplicate]
In Java, why doesn't the following line of code work?
List<List<String>> myList = new ArrayList<ArrayList<String>>();
It works if I change it to
List<ArrayList<String&...
30
votes
9
answers
14k
views
Benefits of arrays
As I see it, the advantages of a list over an array are pretty obvious:
Generics provide more precise typing: List<Integer>, List<? extends Number>, List<? super Integer>.
A List ...
28
votes
7
answers
4k
views
Is there a way to avoid loops when adding to a list?
I was wondering a code like this:
List<String> list = new ArrayList<String>();
for(CustomObject co : objects) {
list.add(co.getActualText());
}
Can it be written differently? I ...
28
votes
5
answers
255k
views
List<Object> and List<?>
I have two questions, actaully...
First off, Why cant I do this:
List<Object> object = new List<Object>();
And second, I have a method that returns a List<?>, how would I turn that ...
28
votes
2
answers
54k
views
Modifier static is only allowed in constant variable declarations
I have an inner class that stores the info of the controls I'm using for a game, now I want to store a static ArrayList in it that holds all the names of the controls. But I am getting this error: &...
28
votes
7
answers
117k
views
convert string to arraylist <Character> in java
How to convert a String without separator to an ArrayList<Character>.
My String is like this:
String str = "abcd..."
I know one way of doing this is converting the String to char[] first, and ...
26
votes
4
answers
94k
views
new ArrayList<int>() failing in Java
I have the following code:
List<int> intList = new ArrayList<int>();
for (int index = 0; index < ints.length; index++)
{
intList.add(ints[index]);
}
It gives me an error...
...
26
votes
7
answers
104k
views
Check if an ArrayList contains a given object
Assuming I have class like this:
class A {
int elementA;
int elementB
}
I also have an ArrayList like this: ArrayList<A> listObj.
How can I check if that list contains an object using ...
25
votes
4
answers
68k
views
How to remove common values from two array lists
How can we remove common values from two ArrayLists?
Let’s consider I have two Arraylist as shown below:
ArrayList1 = [1,2,3,4]
ArrayList1 = [2,3,4,6,7]
I would like to have result as:
...
25
votes
6
answers
59k
views
What is a List vs. an ArrayList? [duplicate]
What are the fundamental differences between the two objects? Is one more efficient? Does one have more methods?
25
votes
3
answers
47k
views
How do I initialize a two-dimensional List statically?
How can I initialize a multidimensional List statically?
This works:
List<List<Integer>> list = new ArrayList<List<Integer>>();
But I'd like to init the list with some ...
24
votes
4
answers
49k
views
Moving data from a HashSet to ArrayList in Java
I have the following Set in Java:
Set< Set<String> > SetTemp = new HashSet< Set<String> >();
and I want to move its data to an ArrayList:
ArrayList< ArrayList< String &...
24
votes
2
answers
27k
views
ArrayList vs the List returned by Arrays.asList() [duplicate]
The method Arrays.asList(<T>...A) returns a List representation of A.
The returned object here is a List backed by an array, but is not an ArrayList object.
I'm looking for the differences ...