-1

i am new with generics and i have a problem with creating an arrays of generic class.

if i have one and i want to create in a seperate main at the same java project generic array wich answer two diffrent types-string and integer.

can i put two difreent types the same array?

and is it ok to call the main like this:

public static <T> void main(String[] args) {

    String name;
    int grade;
    int choice;
    int count = 0;
    String number;

    System.out.println("press 1 if you like to save id as integer ,or press 2 to for a string ");
    choice = s.nextInt();

    Student<T>[] array = new Student[NUM_OF_STUDENTS];

    if (choice == 1) {

        System.out.println("please enter student name :");
        s.nextLine();
        name = s.nextLine();

        while (!name.isEmpty() && count != NUM_OF_STUDENTS) {
            System.out.println("please enter student #" + (count + 1) + " grade :");
            grade = s.nextInt();
            array[count] = new Student(count + 1, name, grade);
            count++;
            ...

....

Andy Turner
  • 122,430
  • 10
  • 138
  • 216
dan
  • 61
  • 1
  • 7
  • 1
    Why is student even generic? What do the generics allow you to do? – Andy Turner Jul 18 '16 at 20:34
  • the class student is generic beacause it aloows to add the student id as integer or string as the user request. – dan Jul 18 '16 at 20:35
  • In that case, a much simpler solution would be to save it as a string, but have a ```getAsInt``` method that parses the id. (But you're probably doing this to learn so...) – Jorn Vernee Jul 18 '16 at 20:37
  • i cant use a generic array wich include both??there isnt such thing in generic? – dan Jul 18 '16 at 20:39
  • Okay, so this is terrible by design, if you must make his ID to just be String... you can convert any integer to string, but it looks like in your case only integer is good.... there is such things as just having Student with 2 different constructors, but it is really not necessary in this case – olexity Jul 18 '16 at 20:39
  • 3
    Generics and arrays are much harder to make work together than just sticking with generics: `List> list = new ArrayList<>();`. – Andy Turner Jul 18 '16 at 20:40
  • 1
    And if you can choose whether the ID is an `Integer` or a `String` in this method, the array elements would have to be of type `Student>`, since they could be either type. The type variable is already "set" by the time you choose the kind of ID. – Andy Turner Jul 18 '16 at 20:42
  • I could not understand exact question of yours .Arrays need information of its content type during runtime, unlike Collection classes. So generics can be used in list, which will eventually lost during run time ( type erasure) . So unlike list, you can not have that type of information in array. Also look at invariant vs covariant for additional details – Jimmy Jul 18 '16 at 20:47
  • There is no point for `main` to be generic when `T` is not used in the parameter or return types – newacct Jul 19 '16 at 21:07

1 Answers1

-1

According to Oracle, no you cannot have generics arrays.

P.S.: you forgot to initialize s, which I am assuming is a java.util.Scanner,

  Scanner s = new Scanner(System.in);

Also don't do:

System.out.println("please enter student name :");
s.nextLine();
name=s.nextLine()

Just do:

System.out.println("please enter student name :");
name=s.nextLine()

Otherwise s.nextLine() skips one line.

Sean Letendre
  • 2,106
  • 2
  • 12
  • 27
  • That floating `s.nextLine()` is necessary. – Sotirios Delimanolis Jul 18 '16 at 21:18
  • 1
    `public static void main(String[] args) { ... }` compiles and runs fine for me. It isn't useful, but it is legal. Don't forget that the JVM only sees the erasure of the method, which is identical to `public static void main(String[] args)`. – Andy Turner Jul 18 '16 at 21:18
  • Also, `String... args` is legal too. – Andy Turner Jul 18 '16 at 21:20
  • Considering the comments above, your answer basically boils down to _According to Oracle, no you cannot have generics arrays._ That's not very helpful. Please extract the parts of the document that explain that generic arrays are not possible and why. – Sotirios Delimanolis Jul 18 '16 at 21:24
  • How is the `s.nextLine();` necessary? Running this (well, a shortened test version) I had to type something for the first _nextLine_, press enter and then type something again for the second _nextLine_ which was then stores as name. Using only `name = s.nextLine();` worked fine. – Sean Letendre Jul 18 '16 at 21:37
  • 1
    Because the `nextInt` leaves a dangling new line character, assuming they pressed enter after providing input. See [here](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods?rq=1). – Sotirios Delimanolis Jul 18 '16 at 21:57