In Java, what do arrays inherit? I can do it?

Sorry for the newbie question, I'm used to C #, so knowing the Java infrastructure is not so good.

I have several arrays:

int[] numbers = new int[10]; String[] names = new String[10]; //populate the arrays 

Now I want to create a generic function that will print 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 have to do now is

 PrintAll(names); PrintAll(numbers); 

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

Many thanks

Bones

+4
source share
5 answers

Arrays implement only Serializable and Cloneable in Java 1 ; therefore there is no general way to do this. You will need to implement a separate method for each type of array (since primitive arrays of type int[] cannot be added to Object[] ).

But in this case you do not need it, because Arrays can do it for you:

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

This will give something like:

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

If this is not so good, you are stuck to implement a version of your method for every possible array type, for example Arrays .

 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); } // ... 

Please note that this 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 .

+6
source

According to other answers, int[] and String[] do not have a common superclass that allows you to do this. One thing you can do is combine the arrays into a list before passing them to your PrintAll() function. This is easy to do using Arrays.asList(myArray) . Then your PrintAll() function can take a Collection or Iterable and repeat it that way.

+2
source

You can try the following.

(This will not work for an int type, since it is a primitive type. You can use an Integer object instead.)

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

To answer the question of what class look at the documents . java.lang.Object is the answer.

In terms of things you should know about iteration. Look at Java enhanced for each statement , and the Iterable Iterable<E> Interface . As others comment, unfortunately, Array does not implement Iterable<E> .

0
source

Here you can find what is the superclass of the array (which is a regular 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 a solution, but the answer to the question (at least the name).
(I would suggest Arrays.toString, as mmyers already did)

0
source

All Articles