All Questions

Tagged with
Filter by
Sorted by
Tagged with
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....
colymore's user avatar
  • 12.1k
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 = ...
Pentium10's user avatar
  • 206k
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?
user25778's user avatar
  • 5,981
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?
scatman's user avatar
  • 14.3k
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); ...
Himanshu's user avatar
  • 4,821
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 ...
Ernestas Gruodis's user avatar
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')...
FAjir's user avatar
  • 4,404
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?...
Jessy's user avatar
  • 15.5k
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 ...
faceless1_14's user avatar
  • 7,522
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 ((...
B. Clay Shannon-B. Crow Raven's user avatar
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 ...
Ariel Magbanua's user avatar
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 ...
Joonas Pulakka's user avatar
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 = ...
Skipper07's user avatar
  • 1,261
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 ...
Saatana's user avatar
  • 1,177
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 ...
trama's user avatar
  • 1,301
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 ...
Debacle's user avatar
  • 1,211
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 ...
AKIWEB's user avatar
  • 19.4k
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 ...
Pranav's user avatar
  • 821
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 ...
arvin_codeHunk's user avatar
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?
sparkyspider's user avatar
  • 13.3k
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>(); /...
James Lawruk's user avatar
  • 30.7k
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&...
abhi's user avatar
  • 1,584
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 ...
Captain Franz's user avatar
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 ...
user avatar
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 ...
bguiz's user avatar
  • 28k
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<...
faulty's user avatar
  • 8,257
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 ...
Kraken's user avatar
  • 23.8k
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
Scott's user avatar
  • 999
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 ...
kCC's user avatar
  • 577
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 ...
prabu's user avatar
  • 1,267
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 ...
IS_EV's user avatar
  • 998
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 ...
Lokesh Pandey's user avatar
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. ...
artouiros's user avatar
  • 3,957
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 ...
Priyank Doshi's user avatar
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 ...
codepleb's user avatar
  • 10.3k
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?
snake plissken's user avatar
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'...
Sergey's user avatar
  • 2,880
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&...
Nosrettap's user avatar
  • 11.1k
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 ...
Vitalii Fedorenko's user avatar
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 ...
Jim's user avatar
  • 19.1k
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 ...
Christopher's user avatar
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: &...
Timotheus's user avatar
  • 2,290
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 ...
Pan Long's user avatar
  • 1,024
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... ...
Alan2's user avatar
  • 24k
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 ...
Saulius S's user avatar
  • 401
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: ...
Gautam's user avatar
  • 3,336
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?
user812892's user avatar
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 ...
cody's user avatar
  • 6,569
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 &...
Ghadeer's user avatar
  • 618
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 ...
Roam's user avatar
  • 4,891

1
2 3 4 5
49