In Java what do arrays inherit from? Can I do this?
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
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.
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.
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);
}
}
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)
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
As others have commented, unfortunately Array does not implement Iterable<E>
.Iterable<E>
.
精彩评论