【问题标题】:How to write a method that reads particular content in an array lists and count the number of array's content如何编写读取数组列表中特定内容并计算数组内容数量的方法
【发布时间】:2020-08-25 17:03:39
【问题描述】:

我是一个新的学习者,我应该创建一个方法来使这个程序完全没有问题地运行,这样我就可以得到如下所示的 :::final 结果:::
3是3
马克就是马克
理查德就是理查德

这是代码(请阅读我在代码中写的 cmets)

public class Main {

/*I wrote the following method (Student) but I keep get some issues:
Please help I spend many days and I can't figure it out and I am runnung out
of time as I should understand the problem or at least the correction that I
can read and figure out what I was doing wrong.*/

// My written code starts here
    public static String Student(String[] sx){
        int counter = 0;
        for (int i = 0; i < sx.length; i ++){
            if (sx[i] != null)  counter ++;
        }
        return counter;
        sx = new String[counter];
    }
// My written code ENDS here

    // From this point I should preserve the code without any changes 
    static Student studentA;
    static Student studentB;
    static Student studentC;

    public static void main(String[] args) {
        studentA = new Student("Mark", "John", "Jimmy");
        studentB = new Student("Will", "George", "Androw");
        studentC = new Student("Frank", "Sam");
        int totalStudents = Student.getTotalStudents();
        System.out.println(totalStudents + " is 3");
        System.out.println(studentA.getFirstName() + " is Mark");
        studentA.setFirstName("Richard");
        System.out.println(studentA.getFirstName() + " is Richard");
    }
}

【问题讨论】:

    标签: java arrays arraylist methods


    【解决方案1】:

    检查以下代码sn-p

    import java.util.List;
    
    public class Main {
    
        // My written code starts here
        static class Student {
            String firstName;
            String middleName;
            String lastName;
    
            // Constructor for setting the class variable with all 3 field
            public Student(String firstName, String middleName, String lastName) {
                this.firstName = firstName;
                this.middleName = middleName;
                this.lastName = lastName;
            }
    
            // Constructor for setting the class variable with 2 field
            public Student(String firstName, String lastName) {
                this.firstName = firstName;
                this.lastName = lastName;
            }
    
            //to get the FirstName field of a student object
            public String getFirstName() {
                return firstName;
            }
    
            //to set the FirstName field of a student object
            public void setFirstName(String firstName) {
                this.firstName = firstName;
            }
    
            public static int getTotalStudents() {
                // this is wrong way of doing this. 
                // You should ideally send a list Of Student to know the number of students as shown below
                return 3;
            }
    
            public static int getTotalStudentsFromList(List<Student> studentList) {
                return studentList.size();
            }
    
        }
    // My written code ENDS here
    
        // From this point I should preserve the code without any changes
        static Student studentA;
        static Student studentB;
        static Student studentC;
    
        public static void main(String[] args) {
            studentA = new Student("Mark", "John", "Jimmy");
            studentB = new Student("Will", "George", "Androw");
            studentC = new Student("Frank", "Sam");
            int totalStudents = Student.getTotalStudents();
            System.out.println(totalStudents + " is 3");
            System.out.println(studentA.getFirstName() + " is Mark");
            studentA.setFirstName("Richard");
            System.out.println(studentA.getFirstName() + " is Richard");
        }
    }
    

    我添加了可能的 cmets 来解释代码。如果您对理解有任何困难,请告诉我。

    【讨论】:

    • 非常感谢您的帮助、时间和解释。这真的很有帮助,现在理解起来很清楚。再次感谢,祝 Ajay 度过愉快的一天 :)
    猜你喜欢
    • 2016-02-21
    • 1970-01-01
    • 2022-07-26
    • 2014-11-28
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多