如何使用数组,一个值来保存元素数组

时间:2018-04-25 19:41:55

标签: java arrays

   public class Student implements Comparable<Student> {
        String fName;
        String sName;
        String subject[];
        int subjectValuation;
         public Student(String fName, String sName, String[] subject, int subjectValuation) {
            this.fName = fName;
            this.sName = sName;
            this.subject = subject;
            this.subjectValuation = subjectValuation;
        }

        public String[] getSubject() {
            return subject;
        }

        public String getfName(){
            return fName;

        }
        public String getsName(){
            return sName;
        }

        public void setSubject(String[] subject) {
            this.subject = subject;
        }

        public int getSubjectValuation(){
            return subjectValuation;
        }
        public void setSubjectValuation(int subjectValuation){
            this.subjectValuation=subjectValuation;
        }
        public String toString(){
            return " fname "+getfName()+ " sname " +getsName()+ " subjects "+subject+ " valuation "+subjectValuation;
        }
        public boolean equals(Object o){
            if(o!=null && o instanceof Student){
                Student s=(Student) o;
                if(this.getfName().equalsIgnoreCase(s.fName) && this.getsName().equalsIgnoreCase(s.sName))
                    return true;
                else
                    return false;
            }
            return false;
        }
        public int compareTo(Student o){
            return this.subjectValuation-o.subjectValuation;
        }


        }






              import java.util.ArrayList;
              import java.util.Collections;
              import java.util.Iterator;

               public class Catalogue {
              private ArrayList<Student> s=new ArrayList<>();
              //performing 1.
            void add(String firstName,String secondName){
                s.add(new Student(firstName,secondName,[0],0));
            }
              //performing 2
            void add2(String firstname,String secondName,String subject[],int valuation){
                Student accSearch=search(firstname,secondName);
                if(accSearch!=null) {
                    accSearch.setSubject(subject);
                    accSearch.setSubjectValuation(valuation);
                }
                else{
                    System.out.println("couldn't find it");
                }

            }
                 //performing 3
            void delete(String firstName,String secondName){
                Student n=new Student(firstName,secondName,[0],0);
                    while(s.contains(n)) {
                        s.remove(n);
                    }

                }
                   //searching my Student
            public Student search(String firstName,String secondName){
                for(Student m:s){
                    if(m.getfName().equalsIgnoreCase(firstName) && m.getsName().equalsIgnoreCase(secondName))
                        return m;
                }
                return null;
            }
                public void listInformation(String firstName,String secondName){
                  for(Student m:s){
                      if(m.getfName().equalsIgnoreCase(firstName) && 
              m.getsName().equalsIgnoreCase(secondName))
                        System.out.println(m);

                }
            }

            void printall(){
                for(Student m:s){
                    System.out.println(m);
                }
            }
                  //performing 4.
            void listValuation(String firstName,String secondName) {
                Student searchingFor = search(firstName, secondName);
                Iterator<Student> i = s.iterator();
                while (i.hasNext()) {
                    Student s = i.next();
                    if (s.getfName().equalsIgnoreCase(firstName) && s.getsName().equalsIgnoreCase(secondName))
                        System.out.println(s.getSubjectValuation());
                }
             ``}
                  //the average not sure how am i going to do it
             void average(String firstName,String secondName){
                Student s=search(firstName,secondName);


            }
             void sort(){
                Collections.sort(s);



             }
           }
public static void main(String[] args) {
        Catalogue c=new Catalogue();
        c.add("a","b");
        c.add("b","c");
        c.add("d","f");
        c.add("a","b");
       // c.listInformation("a","b");
        c.delete("a","b");
        System.out.println("+");
        c.printall();
        c.add2("d","f","math",9);
        c.printall();
        c.add2("f","a","asdsdfd",1);
        System.out.println("++");
        c.printall();
        System.out.println("++++");
       c.listValuation("d","f");
   }
}

上面的类是我的目录类。

我必须执行以下任务:

  1. 添加学生;
  2. 添加学生科目和笔记;
  3. 删除学生;
  4. 在姓名后搜索学生并打印每个科目的每个年级;
  5. 在名字后搜索学生并计算主题的平均值;
  6. 我不知道如何为学生分配一系列科目和成绩,例如我有一个名叫Jon Jon的学生,他可以有多个科目,如Math1,Math2,Math3 ....以及Math1等级的成绩是1 Mah2等级是2 .....

    5.没有实现,但我对如何实现主题和注释的数组更感兴趣

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你就会问如何将主题和成绩结合起来并将它们放到一个数组中。 解决这个问题的一种方法是实现一个单独的班级Subject,该班级的成绩为attribute

public class Subject {
    private String subject;
    private float grade;

    //getter and setter methods
}

您可以在班级Student中使用此课程:

public class Student implements Comparable<Student> {
        String fName;
        String sName;
        Subject subject[]; //array which can contain multiple subjects. each 
                           //entry got two attributes: the subject itself as String; the grade

        //your code
}