Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java what do arrays inherit from? Can I do this?

Tags:

java

arrays

c#

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

like image 374
dbones Avatar asked Dec 08 '09 16:12

dbones


People also ask

Can you inherit from array?

Inheriting from Array<T> If a browser supports iterables, then all built-in collections are implemented as iterables. In particular, it is often very useful to inherit from arrays because JavaScript has a lot of built-in operations to process arrays that will be automatically inherited by all array subclasses.

What can you do with arrays in Java?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

How arrays are passed in Java?

Everything in Java is passed by value. In case of an array (which is nothing but an Object), the array reference is passed by value (just like an object reference is passed by value). When you pass an array to other method, actually the reference to that array is copied.

What inherits from Object in Java?

Every class in Java IS an Object. They behave like Objects, they can be added to collections of type Object, they can use any method defined in Object. So, YES, everything (except primitives) inherit from Object in Java. EDIT:Java takes the approach of "Everything is an Object".


1 Answers

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.

like image 52
Michael Myers Avatar answered Oct 29 '22 08:10

Michael Myers