All Questions

-7
votes
2answers
32819 views

How to avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException?

If your question is I am getting a java.lang.ArrayIndexOutOfBoundsException in my code and I do not understand why it is happening. What does it mean and how can I avoid it? This is meant to be th...
54
votes
4answers
26316 views

Why does my ArrayList contain N copies of the last item added to the list?

I'm adding three different objects to an ArrayList, but the list contains three copies of the last object I added. For example: for (Foo f : list) { System.out.println(f.getValue()); } Expe...
2391
votes
30answers
826012 views

When to use LinkedList over ArrayList?

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 ...
235
votes
22answers
247547 views

Save ArrayList to SharedPreferences

I have an ArrayList with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves the activity and then wants to come back...
366
votes
33answers
549079 views

How do I remove repeated elements from ArrayList?

I have an ArrayList of Strings, and I want to remove repeated strings from it. How can I do this?
2730
votes
32answers
1149331 views

Create ArrayList from array

I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)}; I would like to convert this array into an object of the ArrayList class. ArrayList<...
93
votes
9answers
130743 views

Sorting an ArrayList of objects using a custom sorting order

I am looking to implement a sort feature for my address book application. I want to sort an ArrayList<Contact> contactArray. Contact is a class which contains four fields: name, home number, mo...
1919
votes
32answers
2101041 views

Initialization of an ArrayList in one line

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"); plac...
816
votes
6answers
1077674 views

Convert ArrayList<String> to String[] array

I'm working in the android environment and have tried the following code, but it doesn't seem to be working. String [] stockArr = (String[]) stock_list.toArray(); If I define as follows: String []...
77
votes
9answers
495265 views

Java: How to read a text file

I want to read a text file containing space separated values. Values are integers. How can I read it and put it in an array list? Here is an example of contents of the text file: 1 62 4 55 5 6 77 ...

Previous Next