将对象插入 Java 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26662887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:01:34  来源:igfitidea点击:

Insert Object into Java Array

javaarraysclassobjectconstructor

提问by Ambassador

I got from my teacher the next assignment -

我从老师那里得到了下一个作业——

I need to build a Courseclass and Studentclass and to insert every Student into the Course class Each Student has an Id,name and grade.

我需要建立一个Course班级和Student班级,并将每个学生插入课程班级每个学生都有一个 ID、姓名和成绩。

I have tried the next code:

我已经尝试了下一个代码:

 public class Course {

    Student[] android = new Student[100];


    private void addStudent(Student a) {
        for (int i=0;i<android.length;i++) {
            if (android[i] == null) {
                android[i] = a;
                break;
            }
        }

    }

    public static void main(String[] args) {
        addStudent(Joe);

    }

}

I need to insert a Student that i have created in the Students class to the first null position in the array. When i try addStudent(Joe);it gives me an error : "Joe cannot be resolved to a variable"

我需要将我在 Students 类中创建的 Student 插入到数组中的第一个空位置。当我尝试时,addStudent(Joe);它给了我一个错误:“Joe 无法解析为变量”

The Student class code:

学生类代码:

    public class Student {


                private float grade;
                private String name;
                private long id;
           public Student(long c,String b,float a) {
               grade = a;
               name = b;
               id = c;
           }
             public static void main(String[] args) {
             Student Joe = new Student(1,"Joe",40);
}

**The array holds 100 students (null at start)

**该数组可容纳 100 名学生(开始时为 null)

  • When adding a student - i need to check for the first null value in the array and put it there

  • When printing the students: i need to print only the non-null Objects in the array**

  • 添加学生时 - 我需要检查数组中的第一个空值并将其放在那里

  • 打印学生时:我只需要打印数组中的非空对象**

采纳答案by azurefrog

This code

这段代码

Array[] Android = new Array[100];

is creating an array of type Array, so you can only store Arrayobjects in it.
If you want to store Students, you need to create an array that, instead:

正在创建一个类型为 的数组Array,因此您只能Array在其中存储对象。
如果要存储Students,则需要创建一个数组,而不是:

Student[] android = new Student[100];

Also, you need to realize that arrays in Java are indexed from 0. That is, you cannot reference a position that is the same as the size of the array. In your case, you have made an array with 100 elements, but your for-loop is trying to put 101 objects in it.

此外,您需要意识到 Java 中的数组是从 0 开始索引的。也就是说,您不能引用与数组大小相同的位置。在您的情况下,您创建了一个包含 100 个元素的数组,但您的 for 循环试图将 101 个对象放入其中。

Furthermore, your question text implies you only want to insert the new Studentobject once, but your loop puts it into every empty location in your array.

此外,您的问题文本暗示您只想插入新Student对象一次,但您的循环将其放入数组中的每个空位置。

Try this instead:

试试这个:

    for (int i=0;i<android.length;i++) { // < instead of <=, don't hardcode the length
        if (android[i] == null) {
            android[i] = a;
            break;                       // once we insert a, stop looping
        }
    }

Update

更新

The reason that the compiler can't find Joeis an issue of scope. You have declared Joeas a localvariable in the main()method in the Studentclass. If you want the compiler to be able to see it, you need to declare it in the same method you are using it:

编译器找不到的原因Joescope的问题。您已在类中的方法中声明Joe局部变量。如果您希望编译器能够看到它,您需要在您使用它的相同方法中声明它:main()Student

public static void main(String[] args) {
    Student Joe = new Student(1,"Joe",40);
    addStudent(Joe);
}

A quick google search of "Java variable scope tutorial" should give you plenty of reading about how and when you can use local and member variables.

谷歌快速搜索“Java 变量范围教程”应该会给你大量关于如何以及何时可以使用本地和成员变量的阅读。

回答by AlvinJ

First, change the

首先,改变

Array[] Android = new Array[100]; 

to

Student[] listOfStudent = new Student[100];

What this does is creates a array of size 100, and the type of the array is Students, which is what you will be inserting into the array. You are also going to have to change

它的作用是创建一个大小为 100 的数组,该数组的类型是 Students,这就是您将要插入到该数组中的内容。你也将不得不改变

for (int i=0;i<=100;i++) {
        if (Android[i] == null) {
            Android[i] = a;

        }
}

to

for(int i=0; i < listOfStudents.length; i++){
    if(listOfStudents[i] == null){
        listOfStudent[i] = a;
    }
}

otherwise you will get a "List Out of Bounds" error. It is better to not hard code values into your code - good practice for the future!

否则,您将收到“列表越界”错误。最好不要将值硬编码到您的代码中 - 未来的好习惯!

Good Luck!

祝你好运!

回答by Ahmed

insert method of student S (1111,"ahmed",3.5)in the position "p" in array of students(id,name,GBG)array size 100 and curent size =3.

将学生 S (1111,"ahmed",3.5) 的方法插入到学生数组 (id,name,GBG) 数组大小为 100 且当前大小 =3 的位置 "p" 中。

-remove method that remove the student having GBG =2.5from the array of student .array size 100 and currentsize=3

-remove 方法从学生数组中删除 GBG =2.5 的学生 .array 大小为 100 和 currentsize=3