2017-01-26 5 views
-4

オブジェクト私はコースの文字列の配列の初期化を理解することはできませんので、誰かが、このクラスで何が起こっているのか私に説明し、成績Javaクラスは、指向プログラミング

public class stud{ 
    int id; 
    String nam; 
    String courses[]; 
    double grades[]; 
    int maxSize, size; 

    public stud(int d, String n, int ms) 
    { 
    id = d; 
    nam = n; 
    courses = new String[ms]; 
    grades = new double[ms]; 
    size=0; 
    } 
    public void addCourse(String nc, double g) 
    { 
    courses[size]=nc; 
    grades[size]=g; 
    size++; 
    } 
    public void print() 
    { 
    System.out.println(id+ " "+nam); 
    for(int i=0; i<size; i++) 
     System.out.println(courses[i]+ " "+grades[i]); 
    } 
} 
+0

でライン 'コース=新しいString [ミリ秒]を見たいと思うかもしれませんもある;' 'ms'を保存することができ、アレイ' courses'にメモリを割り当ていいえ。値の –

+0

ええ、構文的に意味的に理解していないのでしょうか? – DuKes0mE

+0

あなたは本当にドキュメントを読んだり、いくつかのチュートリアルを見る必要があります。文字通りちょうどGoogleの "Java配列のチュートリアル"。明らかな研究努力の欠如のため-1。 – tnw

答えて

0

は、以下のいくつかのコメントを追加しましたことができます。

public class stud { 
    int id; 
    String nam; 
    String courses[]; 
    double grades[]; 
    int maxSize, size; 

    public stud(int d, String n, int ms) 
    { 
     // initialise the attributes 
     id = d; 
     nam = n; 

     // create empty arrays with a size of ms 
     courses = new String[ms]; 
     grades = new double[ms]; 

     //point to the first item in the array 
     //Also gives the number of values in the array 
     size=0; 
    } 

    public void addCourse(String nc, double g) 
    { 
     //Add a course name and grade into the array 
     //they are added at the location pointed to by 'size' 
     courses[size]=nc; 
     grades[size]=g; 

     //Increment the pointer to the next empty array location 
     size++; 
    } 

    public void print() 
    { 
     System.out.println(id+ " "+nam); 

     //Iterate over the arrays until we get to the size 
     for(int i=0; i<size; i++) 
      System.out.println(courses[i]+ " "+grades[i]); 
    } 
} 

出力としては何も出力されません。 addCourseメソッドは、コースを追加するために呼び出す必要があります。

これはかなりひどいコード化され、あなたが

Map<String, Double> 
関連する問題