arraylist

Why is it preferred to use Lists instead of Arrays in Java?

Many people and authors suggested to us to use list than array. List <Integer> list = new ArrayList<Integer>(); list.addElement(1); .... What it is the reason behind it? ...

difference between hashmap and array list in java?

In java, ArrayList and HashMap are using in collections. But i couldn't understand at which situations we should use ArrayList and which time use HashMap. What is the major difference between both of them? ...

Remove duplicates from a sorted ArrayList while keeping some elements from the duplicates

Okay at first I thought this would be pretty straightforward. But I can't think of an efficient way to solve this. I figured a brute force way to solve this but that's not very elegant. I have an ArrayList. Contacts is a VO class that has multiple members - name, regions, id. There are duplicates in ArrayList because different regions ap...

How to combine elements of a list

I'm working in c#. I have a sorted List of structures. The structure has a DateTime object which stores month and year and an integer which stores a value. The list is sorted by date. I need to traverse the list and combine it so that I only have one instance of the structure per date. For example: My initial list would look like this:...

Java: The difference between iterators and arraylists

How would you explain to someone who has just started programming in Java, what the difference between ArrayLists and Iterators are? Why I would use an iterator instead of using the get() methods of the Arraylist ...

make ArrayList Read only

How to make a Array List read only, so no one can add, edit or delete, once after the initialization in Java ...

What are the implications of casting a generic List to a non-generic List?

I'm refatoring a home-grown DAO container, hoping to make the class generic. It internally uses an ArrayList to store the retrieved objects. One usage of this class puts the container's list into a request scope, and due to a limitation of Websphere, I can't pass the generic List<Foo> to the request scope (Websphere doesn't handle gener...

Variable length (Dynamic) Arrays in Java

I was wondering how to initialise an integer array such that it's size and values change through out the execution of my program, any suggestions? ...

Java - Removing duplicates in an ArrayList

I'm working on a program that uses an ArrayList to store Strings. The program prompts the user with a menu and allows the user to choose an operation to perform. Such operations are adding Strings to the List, printing the entries etc. What I want to be able to do is create a method called removeDuplicates().This method will search the A...

Java: omitting a data member from the equals method.

public class GamePiece { public GamePiece(char cLetter, int nPointValue) { m_cLetter=cLetter; m_nPointValue=nPointValue; m_nTurnPlaced=0; //has not been placed on game board yet. } public char GetLetter() {return m_cLetter;} public int GetPointValue() {return m_nPointValue;} public int GetT...

sql statement supposed to have 2 distinct rows, but only 1 is returned

I have an sql statement that is supposed to return 2 rows. the first with psychological_id = 1, and the second, psychological_id = 2. here is the sql statement select * from psychological where patient_id = 12 and symptom = 'delire'; But with this code, with which I populate an array list with what is supposed to be 2 different rows,...

How do I make my ArrayList Thread-Safe? Another approach to problem in Java?

I have an ArrayList that I want to use to hold RaceCar objects that extend the Thread class as soon as they are finished executing. A class, called Race, handles this ArrayList using a callback method that the RaceCar object calls when it is finished executing. The callback method, addFinisher(RaceCar finisher), adds the RaceCar object t...

How to check the type of object in ArrayList

Hi, Is there a way to get the type of object in the arraylist? I need to make an IF statment as the following (in C#): if(object is int) //code else //code thanks ...

Add elements to Arraylist and it replaces all previous elements in Java

I am adding elements to a ArrayList and it adds the first one correctly but then when I add any subsequent elements it wipes replaces the other elements with the value from the most recently added and adds a new element to the ArrayList. I ran test using arraylist and ints and even another created class and it worked perfectly but somet...

Java - problems with polymorphism

I have a book class, then a novel- and a science book class that extend the book class. I made an ArrayList using the book class, then inserted the novels and the science books into that. Now I'm trying to iterate through the ArrayList to count how many novels are there. How can I tell? Would love to see some examples of this! I've bee...

Convert Java Arraylist return from Java WebService to C# Arraylist

In my C# program, I would like to consume the Java Web Service Method which replies the java.Util.ArrayList. I don't know how to convert the Java ArrayList to C# ArrayList. This is Java Codes @WebMethod @WebResult(name = "GetLastMessages") @SuppressWarnings("unchecked") public ArrayList<Message> getLastMessages(@WebParam(name = "Messa...

Should I use an ArrayList or IList

Im using the .NET framework 1.1 and Im hoping someone could help me implement a dynamic array of objects? A watered-down example of the object I wish use is below. Class CarObj { public string CarName; public string CarYear; } Should I use an ArrayList or do you think it would be better to make a CarList class to implement an...

Does the List in .NET work the same way as arraylist in Java?

When I learned Java, I was told that the arraylist works this way: It creates an array with room for 10 elements. When the 11th element is added, it is created a new list with room for 20 elements, and the 10 elements are copied into the new array. This will repeat as until there are no more elements to add, or to a maximum size. Is ...

Passing parametrized ArrayList to a function in java

I have the following fucntion. func(ArrayList <String>[] name) { ........ } The function fills the ArrayList[]. (I don't want to return the ArrayList[]) However, in the caller function the ArrayList[] obtained has all ArrayLists as null. For eg. 1 name = new ArrayList[num]; 2 func(name); 3 System.out.println(name[0]); I get NullPo...

Error when starting an activity

I'm starting an activity when a button is pressed, and normally (in other apps) haven't had an issue. But when I press the button in this app, I get an "unable to marshal value" error. Exact(ish) error from LogCat: 03-22 02:49:02.883: WARN/System.err(252): java.lang.RuntimeException: Parcel: unable to marshal value {CLASSNA...