2016-10-10 38 views
-4

我已經創建了一個類「學生」現在我試圖將我的Student對象存儲在一個ArrayList中,但編譯時ArrayList沒有接受參數。我查了很多次我的代碼,但無法找到問題。獲取錯誤java.util中的ArrayList不帶參數

Error Picture

我的學生類是這個

public class Student 
{ 
//declaring variables 
private int rollNumber ; 
private String name ; 


//a counter for counting how many students or objects are declaired yet 
public static int studentCount = 0; 
public static int getStudentCount() 
{ 
    return studentCount; 
} 


//getters for both variables 
public int getRollNumber() 
{ 
    return rollNumber; 
} 
public String getName() 
{ 
    return name; 
} 


//setters for both variables 
public void setRollNumber(int x) 
{ 
    if (x > 0) 
    { 
     this.rollNumber = x; 
    } 
    else 
    { 
     System.out.println("Wrong Roll Number Value"); 
    } 
} 
public void setName(String someName) 
{ 
    this.name = someName; 
} 


//default constructor 
public Student() 
{ 
    setName("Please Enter A Name"); 
    this.rollNumber = 0; 
    studentCount +=1; 
} 


//parameterized constructor 
public Student(String name, int x) 
{ 
    setName(name); 
    setRollNumber(x); 
    studentCount += 1; 
} 


//copy constructor 
public Student(Student s) 
{ 
    setName(s.name); 
    setRollNumber(s.rollNumber); 
    studentCount +=1; 
} 

//defining an print method 
public void print() 
{ 
    System.out.println("Student Name = " + name + "______Roll Number = " + rollNumber); 
} 
} 

和我在這家implimenting是如下

import java.util.*; 

public class ArrayList 
{ 
    public static void main (String[] anyString) 
{ 
    //Creating Some student objects 
    Student s1 = new Student("Ammar" , 1); 
    Student s2 = new Student("Ahmad" , 2); 
    Student s3 = new Student("Arslan", 3); 


    //Creating an arraylist 
    ArrayList<Student> someList= new ArrayList<Student>(); 


    //checking if our Array List is empty or not 
    boolean empty = someList.isEmpty(); 
    if (empty == true) 
     System.out.println("ArrayList is Empty \n\n"); 


    //adding our student class objects to our arraylist 
    someList.add(s1); 
    someList.add(s2); 
    someList.add(s3); 


    //checking if arraylist is empty or not 
    empty = someList.isEmpty(); 
    if (empty == true) 
     System.out.println("ArrayList is Empty"); 
    else 
    //counting total members in a list 
    System.out.println("Our ArrayList someList have a total of " +someList.size() + " Members \n\n"); 


    //geting back objects from arraylist or extractin them from list 
    //we will display our objects to console one by one 
    for (int i = 0; i < someList.size() ; i++) 
    { 
     Student one = someList.get(i); 
     one.print(); 
    } 
} 
} 
+2

'public class ArrayList' ... – Tunaki

+1

您創建的ArrayList類不帶類型參數,它不是泛型類。 (提示:不要給那些已經屬於其他事物的名字。) – David

+3

喲,我聽說你喜歡'ArrayList's。所以我在'ArrayList'中放入了一個'ArrayList',這樣編譯器就會感到困惑。 – Turing85

回答

2

清理,改名爲類。

import java.util.ArrayList; 

public class Student 
{ 
    String name; 

    int id; 

    public Student(String name, int id) 
    { 
    this.name = name; 
    this.id = id; 
    } 

    public static void main(String[] anyString) 
    { 
    // Creating Some student objects 
    Student s1 = new Student("Ammar", 1); 
    Student s2 = new Student("Ahmad", 2); 
    Student s3 = new Student("Arslan", 3); 

    // Creating an arraylist 
    ArrayList<Student> someList = new ArrayList<Student>(); 

    // checking if our Array List is empty or not 
    boolean empty = someList.isEmpty(); 
    if (empty == true) 
     System.out.println("ArrayList is Empty \n\n"); 

    // adding our student class objects to our arraylist 
    someList.add(s1); 
    someList.add(s2); 
    someList.add(s3); 

    // checking if arraylist is empty or not 
    if (someList.isEmpty()) 
     System.out.println("ArrayList is Empty"); 
    else 
     // counting total members in a list 
     System.out.printf("Our ArrayList someList has a total of %d Members \n\n", 
      someList.size()); 

    // geting back objects from arraylist or extracting them from list 
    // we will display our objects to console one by one 
    for (Student student : someList) 
    { 
     System.out.println(student); 
    } 
    } 

    @Override 
    public String toString() 
    { 
    return "Student [name=" + name + ", id=" + id + "]"; 
    } 
} 
+0

好 - 我們希望阿馬爾自己找到它,他會自學很多:) – gusto2

+0

@GabrielVince兄弟。 ..真我也想要靠它,但我堅持這個,因爲兩個小時知道:P 我只是想知道我要改變在 ArrayList someList = new ArrayList (); 如果你建議改變我會非常感謝:) –

+0

@AmmarAhmed我發佈的代碼是你的代碼應該是什麼,除非你試圖實現自己的'ArrayList'。試試吧,注意差異。 – JynXXedRabbitFoot

相關問題