1

Long story short, I need a list of arrays that can be of varying types (int, double, String, byte, etc.). Now the thing is I have gotten this to work with an unspecified ArrayList object, but every time I retrieve an array from the ArrayList, I'm converting it to the type of array I want e.g. ((double[]) arraylist.get(j))[i] then doing what I have to do.

But my worry is: This is a program that will be iterating hundreds of times and I feel like this is very inefficient and slow. Would rather just have a list of references instead of requiring constant conversions. So any thoughts on alternatives would be appreciated.

3
  • 4
    There is no conversion in the line of code you posted. Only casts. Why do you feel it's slow? Have you made measurements? Commented May 18, 2012 at 20:00
  • maybe you're looking for List<List<? extends Object>> or List<Object[]> Commented May 18, 2012 at 20:03
  • You can feel the ineffiency? With which organ? Really: Make a measurement, and compare an Object array List and a core List<Whatever []>. Commented May 18, 2012 at 21:00

2 Answers 2

6

There's no actual conversion done, just a type check on runtime. If you just need to do this a few hundred times per second it shouldn't be much of an issue, though storing unrelated data sounds as if you are attacking your problem from the wrong angle anyways (of course I might be wrong, but I can't think of a situation in which this would be required or preferred).

Sign up to request clarification or add additional context in comments.

2 Comments

Agreed. Hopefully OP can give more details, maybe we can avoid the storing issues by taking another tack.
It's dealing with a custom table class that returns columns as type arrays. As the physics of my model keeps changing I keep having to restructure the table to fit so I want a flexible format where I don't need to be changing every bit of code that deals with the table and instead in just one location. So to have all the column arrays within a single object where it could just loop through them was my line of reasoning.
2

As all others are saying - there is no conversion - its just a cast.

But in case you don't want write this cast you can wrap this listarray in class and provide a method:

public <T> T get(int indexOfArray, int indexInArray) { return (T) ((T[]) arraylist.get(indexOfArray))[indexInArray]; } 

so that you can use within your code:

MyClassWithArrayList l = new MyClassWithArrayList(...); ... double d = l.get(1, 2); 

but of course you must be completely sure that array at 1 index is of double type (there would be cast exceptions)

or if you know that always there will be an array of doubles at 1 index you could write

public interface AppConstants { public static final int DOUBLE_TABLE_INDEX = 1; ... 

and use:

double d = l.get(AppConstants.DOUBLE_TABLE_INDEX, 2); 

3 Comments

Cool, yeah if it's just a cast then I think it's fine as is. It just wasn't returning an array, at least in the sense that I couldn't just do arraylist.get()[i] for example, so I was worried this was throwing in an unnecessary conversion.
Note that the above code not only has even more casts than the original solution, but does conversions from double to Double and from Double to double. And it will also fail if the list contains double[] arrays.
Yes - it will fail on double[] arrays. Because of that you should consider inserting only Object arrays not primitive type arrays.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.