arraylist

UnsupportedOperationException in AbstractList.remove() when operating on ArrayList

这一生的挚爱 提交于 2020-06-24 18:13:15
问题 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 this code: protected void removeZeroLengthStringsFrom(List<String> stringList) { ListIterator<String> iter = stringList.listIterator(); String s; while (iter.hasNext()) { s = iter.next(); if (s.length() == 0) { iter.remove(); } } } What am I missing here? I have verified that the List<String> I am

UnsupportedOperationException in AbstractList.remove() when operating on ArrayList

戏子无情 提交于 2020-06-24 18:11:12
问题 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 this code: protected void removeZeroLengthStringsFrom(List<String> stringList) { ListIterator<String> iter = stringList.listIterator(); String s; while (iter.hasNext()) { s = iter.next(); if (s.length() == 0) { iter.remove(); } } } What am I missing here? I have verified that the List<String> I am

UnsupportedOperationException in AbstractList.remove() when operating on ArrayList

╄→尐↘猪︶ㄣ 提交于 2020-06-24 18:10:03
问题 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 this code: protected void removeZeroLengthStringsFrom(List<String> stringList) { ListIterator<String> iter = stringList.listIterator(); String s; while (iter.hasNext()) { s = iter.next(); if (s.length() == 0) { iter.remove(); } } } What am I missing here? I have verified that the List<String> I am

Is there a way to avoid loops when adding to a list?

大城市里の小女人 提交于 2020-06-24 10:55:06
问题 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 mean of course at some point there will be a loop but I am wondering if there is an API usage I am ignoring 回答1: If you use Java 8, you can take advantage of the Stream API: List<String> list = objects.stream() .map(CustomObject::getActualText) .collect(Collectors.toList()); 回答2: If you have Java 8, what about: objects

Why does the hashCode() of an ArrayList change every time you add a new element?

旧街凉风 提交于 2020-06-24 07:19:16
问题 As per my understanding of ArrayList , the default capacity is 10 and when it grows beyond 10, it will create a new object with new capacity and so on.. So out of curiosity, I typed following program to check hashcode() for ArrayList object: public class TestCoreJava { public static void main(String [] args){ ArrayList al = new ArrayList(); for(int i=0;i<15;i++){ al.add("Temp"+i); System.out.println("Hashcode for "+i+" element "+al.hashCode()); } } } According to above scenario, when I am not

ArrayList and Multithreading in Java

让人想犯罪 __ 提交于 2020-06-24 04:50:06
问题 Under what circumstances would an unsynchronized collection, say an ArrayList, cause a problem? I can't think of any, can someone please give me an example where an ArrayList causes a problem and a Vector solves it? I wrote a program that have 2 threads both modifying an arraylist that has one element. One thread puts "bbb" into the arraylist while the other puts "aaa" into the arraylist. I don't really see an instance where the string is half modified, I am on the right track here? Also, I

How to avoid adding duplicate entries in an arraylist of objects

你离开我真会死。 提交于 2020-06-23 14:10:37
问题 I have a class 'Employee' which has attributes (Designation, DoB, Employee-I'd, Name, Salary). Now through the following function I'm adding 'n' employee objects in the ArrayList 'abc' But I need to modify it in such a way that if an end user tries to add a duplicate employee (If name of two employees are same then it should be considered as duplicate) then the user is not allowed to do so... How can I do this... I think probably using Map Interface... But no idea, how to implement public

How to avoid adding duplicate entries in an arraylist of objects

别说谁变了你拦得住时间么 提交于 2020-06-23 14:10:26
问题 I have a class 'Employee' which has attributes (Designation, DoB, Employee-I'd, Name, Salary). Now through the following function I'm adding 'n' employee objects in the ArrayList 'abc' But I need to modify it in such a way that if an end user tries to add a duplicate employee (If name of two employees are same then it should be considered as duplicate) then the user is not allowed to do so... How can I do this... I think probably using Map Interface... But no idea, how to implement public

Checking if an ArrayList contains a certain String while being case insensitive

℡╲_俬逩灬. 提交于 2020-06-23 06:43:09
问题 How can i search through an ArrayList using the .contains method while being case insensitive? I've tried .containsIgnoreCase but found out that the IgnoreCase method only works for Strings. Here's the method I'm trying to create: private ArrayList<String> Ord = new ArrayList<String>(); public void leggTilOrd(String ord){ if (!Ord.contains(ord)){ Ord.add(ord); } } 回答1: You will need to iterate over the list and check each element. This is what is happening in the contains method. Since you

Checking if an ArrayList contains a certain String while being case insensitive

折月煮酒 提交于 2020-06-23 06:41:59
问题 How can i search through an ArrayList using the .contains method while being case insensitive? I've tried .containsIgnoreCase but found out that the IgnoreCase method only works for Strings. Here's the method I'm trying to create: private ArrayList<String> Ord = new ArrayList<String>(); public void leggTilOrd(String ord){ if (!Ord.contains(ord)){ Ord.add(ord); } } 回答1: You will need to iterate over the list and check each element. This is what is happening in the contains method. Since you