Page 1 of 1

Arrays der generischen Klasse in Java

Posted: 24 Feb 2025, 05:12
by Guest
Ich bin neu mit Generika und ich habe ein Problem beim Erstellen einer Arrays der generischen Klasse. Generisches Array, das zwei verschiedene Typen und Ganzzahl beantworten. die Haupt wie Dies: < /p>

Code: Select all

public static  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[] 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++;
...
....