arraylist

HashMap(key: String, value: ArrayList) returns an Object instead of ArrayList?

I'm storing data in a HashMap with (key: String, value: ArrayList). The part I'm having trouble with declares a new ArrayList "current," searches the HashMap for the String "dictCode," and if found sets current as the returned value ArrayList. ArrayList current = new ArrayList(); if(dictMap.containsKey(dictCode)) { current = d...

search in java ArrayList.

I'm trying to figure out the best way to search a customer in an ArrayList by its Id number. The code below is not working; the compiler tells me that I am missing a return statement. Customer findCustomerByid(int id){ boolean exist=false; if(this.customers.isEmpty()) { return null; } for(int i=0;i<this.custome...

Best way to use contains in an ArrayList in Java?

Hello I have an ArrayList in Java which is made up of a type containing two strings and an integer. I can successfully test if one element of this ArrayList equals another but I find that the contains method fails. I believe this is due to the fact that my type is not primitive. Now I see two alternatives to this and I wonder which is ...

Initialization of an ArrayList in one line.

I am willing to create a list of options to test something. I was doing: ArrayList<String> places = new ArrayList<String>(); places.add("Buenos Aires"); places.add("Córdoba"); places.add("La Plata"); I refactor the code doing: ArrayList<String> places = new ArrayList<String>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata")); I...

Use HashSet over ArrayList to Convey Intention?

Imagine that I need to create a Collection of elements, where order could or could not matter. Effectively all I plan on doing is using the iterator. I notice most of my colleagues using an ArrayList vs LinkedHashSet/HashSet. My question is, if I know that these elements should be unique, should I be using a Set or a List? Effectively it...

Best way to create a hashmap of arraylist

I have one million rows of data in .txt format. the format is very simple. For each row: user1,value1 user2,value2 user3,value3 user1,value4 ... You know what I mean. For each user, it could appear many times, or appear only once (you never know). I need to find out all the values for each user. Because user may appear randomly, I u...

Why collections classes in C# (like ArrayList) inherit from multiple interfaces if one of these interfaces inherits from the remaining?

When I press f12 on the ArrayList keyword to go to metadata generated from vs2008, I found that the generated class declaration as follows public class ArrayList : IList, ICollection, IEnumerable, ICloneable I know that the IList already inherits from ICollection and IEnumerable, so why does ArrayList redundantly inherit from these in...

Why does this nested ArrayList code throw an exception?

ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>(5); for (int i = 0 ; i < a.size() ; i++){ a.set(i, new ArrayList<Integer>(10)); } System.out.println(a.get(a.size()-1).get(9)); //exception thrown The above snippet throws an exception in the printing part. Why? ...

Retrieving data from VB.net arraylist of objects

I am trying to retrieve the correct value from an ArrayList of objects (1.1 Framework) : I have the following defined : Public AlList As New ArrayList Public Class ItemInfo Public ItemNo As Int16 Public ItemType As String Public Reports As Array Public PDFs As Array End Class The form_load event code contains : Dim ...

how to get a column of recordset in an arraylist ?

how to get a column of recordset in an arraylist ?? using vb.net .. please help me out .. ...

Converting an untyped Arraylist to a typed Arraylist

Is there a more elegant solution to convert an 'Arraylist' into a 'Arraylist<Type>'? Current code: ArrayList productsArrayList=getProductsList(); ArrayList<ProductListBean> productList = new ArrayList<ProductListBean>(); for (Object item : productsArrayList) { ProductListBean product = (ProductListBean)item; productList.add(produc...

How to instantiate an ArrayList<?> and add an item through reflection with Java?

I am writing a deserialization method that converts xml to a Java object. I would like to do this dynamically and avoid writing hard coded references to specific types. For example this is a simplified version of one of my classes. public class MyObject { public ArrayList<SubObject> SubObjects = new ArrayList<SubObject>(); } Her...

What is the ideal growth rate for a dynamically allocated array?

C++ has std::vector and Java has ArrayList, and many other languages have their own form of dynamically allocated array. When a dynamic array runs out of space, it gets reallocated into a larger area and the old values are copied into the new array. A question central to the performance of such an array is how fast the array grows in siz...

C# - Check datatype of a value via if condtion

I am binding an ArrayList() to a Listbox control and assigning out an Displaymember and Value on data in the Array. My problem is that I Bind on startup but the Array gets filled after a few function calls. I have code on selectedIndexChanged to check the selectedValue but if the ArrayList is empty it returns an object, once it has data ...

Sort String Array As Int

Is there some way to use IComparer with ArrayList.Sort() to sort a group of strings as ints? ...

Java Performance - ArrayLists versus Arrays for lots of fast reads.

I have a program where I need to make 100,000 to 1,000,000 random-access reads to a List-like object in as little time as possible (as in milliseconds) for a cellular automata-like program. I think the update algorithm I'm using is already optimized (keeps track of active cells efficiently, etc). The Lists do need to change size, but tha...

Shrinking an ArrayList to a new size

Do I really need to implement it myself? private void shrinkListTo(ArrayList<Result> list, int newSize) { for (int i = list.size() - 1; i >= newSize; --i) list.remove(i); } ...

Search arraylist of objects

I have an arraylist of objects, I would like to know the index within the arraylist of the object that contains a certain value, is there a built-in way to do the search? I know I could simply iterate through the arraylist to find the correct value e.g. : ReportToFind="6" For i = 0 To ReportObjList.Count - 1 If ReportObjList.Item(...

Most efficient way to reverse a stack and add to an ArrayList

So, this is an efficiency question. I have two collections - an ArrayList and a Stack. I use the stack because I needed some simple pop/push functionality for this bit of code. The ArrayList is essentially the out variable as this is a small section of code in the function. So, I the variables are defined as such, then code is run to ...

Arrays.asList() of an array

Hi! My question is: what is wrong with this conversion? public int getTheNumber(int[] factors) { ArrayList<Integer> f = new ArrayList(Arrays.asList(factors)); Collections.sort(f); return f.get(0)*f.get(f.size()-1); } I made this after reading solution found in http://stackoverflow.com/questions/157944/how-to-create-array...