It is necessary to implement a method using generalized type parameters, which will take an array as an argument and output it to the console. The input arrays can be of different types - int, float, String . Without using built-in Java methods such as Arrays.toString , etc. How can this be done? The code that I wrote is incorrect. Why?

 public class PrintOutArray<T>{ public static void printOutArray(<T>[] array){ for (int i = 0; i < array.length; i++){ System.out.print(array[i]); } } } 

Closed due to the fact that it was off topic by the participants default locale , 0xdb , user192664, aleksandr barakin , Jarvis_J 8 Nov '18 at 17:02 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - default locale, 0xdb, Community Spirit, aleksandr barakin, Jarvis_J
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • No, you tell me why it is wrong :) Tried to run it? What happened? - default locale
  • Is this some kind of training or test task? Did you write it literally? - Sergey Gornostaev
  • learning task. Idea highlights the red array and writes Cannot resove symbol 'array'. - Dima dimmxx
  • So remove the brackets T [] array - Roman Danilov

1 answer 1

You can even parameterize your method instead of the whole class, so it would be more correct:

 public class PrintOutArray{ public static <T> void printOutArray(T[] array){ for (int i = 0; i < array.length; i++){ System.out.print(array[i]); } } }