2

so I have got a class that has the following method signature

public void doStuff(**Object[] lists**)
{ 
    ...
}

I am having trouble figuring out how to access the individual elements in the lists

For example, if one of the objects passed (say list[0]) is a Character array (Character[] = {'a', 'd', 'z'}) then how would I find out from this method that lists[0] has 'a' 'd' and 'z' in it?

I have tried code like: Object list0 = list[0]; but then I am absolutely lost on how to get the contents of list[0] (in this case, 'a' 'd' and 'z').

Any ideas?

(UPDATE)

Thanks for the responses. I was able to modify your guys' ideas and make it work :)

2
  • 2
    If you know you have arrays in the lists, you can cast the elements. For example, ((Character[])lists[0])[0] Commented Sep 19, 2013 at 23:59
  • If you have an array of arrays, you could use multidimensional arrays (lists[][]) Commented Sep 20, 2013 at 0:01

2 Answers 2

4

Just drill down the param as required:

public void doStuff(Object[] lists) 
{ 
   Object entry0 = lists[0]; //Object Array only contains objects.
   char[] charArr = (char[]) entry0; // Explicit cast: You know that lists[0] is a char Array!
   for (char c : charArr)
   {
     System.out.println("Char Array contains: " + c);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If your arrays have strings: (otherwise replace whatever object you are using)

Object first = lists[0];
ArrayList<String> individual = (ArrayList<String>)first;
String itemOne = individual.get(0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.