tags:

views:

251

answers:

5

Hi

Sorry for the newbie question, I'm used to C# so my Java framework knowledge is not so good.

I have a couple of arrays:

int[] numbers = new int[10];
String[] names = new String[10];

//populate the arrays

Now I want to make a generic function which will print out the values in these arrays, something like the following (this should work in C#)

private void PrintAll(IEnumerable items)
{    
    foreach(object item in items)       
        Console.WriteLine(item.ToString());
}

All I would have to do now is to

PrintAll(names);
PrintAll(numbers);

How can I do this in Java? What is the inheritance tree for the array in Java?

Many thanks

Bones

+1  A: 

To answer the question as to what class- have a look at the docs. java.lang.Object is the answer.

In terms of things you should know about for iteration- Have a look at the Java enhanced for each statement, and Interface Iterable Iterable<E>. As others have commented, unfortunately Array does not implement Iterable<E>.

RichardOD
Sadly, arrays don't implement `Iterable`.
Michael Myers
No they don't, but enhanced for each works with arrays and Iterable<E>.
RichardOD
+6  A: 

Arrays only implement Serializable and Cloneable in Java1; so there is no generic way to do this. You'd have to implement a separate method for each type of array (since primitive arrays like int[] cannot be cast to Object[]).

But in this case, you don't have to because Arrays can do it for you:

System.out.println(Arrays.toString(names));
System.out.println(Arrays.toString(numbers));

This will yield something like:

[Tom, Dick, Harry]
[1, 2, 3, 4]

If that's not good enough, you're stuck having to implement a version of your method for each possible array type, like Arrays does.

public static void printAll(Object[] items) {
    for (Object o : items)
        System.out.println(o);
}
public static void printAll(int[] items) {
    for (int i : items)
        System.out.println(i);
}
public static void printAll(double[] items) {
    for (double d : items)
        System.out.println(d);
}
// ...

Note that the above only applies to arrays. Collection implements Iterable, so you can use:

public static <T> void printAll(Iterable<T> items) {
    for (T t : items)
        System.out.println(t);
}

1 See JLS §10.7 Array Members.

Michael Myers
Thought of the same, but it includes the array string representation; e.g. "[1, 2]". I don't know if that's OK or not.
Yngve Sneen Lindal
There just aren't that many native types, so adding the individual methods shouldn't be that big a burden.
David
There are eight primitives plus Object, so that could get tedious if you're doing more than one method with each.
Michael Myers
+1  A: 

You could try the following.

(It won't work for type int as it is a primitive type. You could use the object Integer instead.)

public void print(Object[] objects){
    for (Object o: objects){
        System.out.println(o);   
    }
}
Gordon
+2  A: 

As the other answers state, int[] and String[] have no common superclass that will let you do it. One thing you can do is wrap the arrays in a list before passing them to your PrintAll() function. This is easily done using Arrays.asList(myArray). Then your PrintAll() function can take in a Collection or Iterable and iterate it that way.

Colin Gislason
Or better yet, an `Iterable`.
Michael Myers
Good point. Edited to add this.
Colin Gislason
A: 

Here's a way to find which is the superclass of an array (which is a normal Object)

    String[] array = {"just", "a", "test"};

    Object obj = array;  // not really needed, just as example
    System.out.println("class: " + obj.getClass());
    System.out.println("super: " + obj.getClass().getSuperclass());

not the solution but answer to the question (title at least).
(I would suggest Arrays.toString as already done by mmyers)

Carlos Heuberger