英文:
Object information does not coincide with Array of Object's index
问题
我试图让函数infoEst() 迭代一个对象数组,并打印出对象的名称和位置。它能正常迭代,但似乎数组中的每个索引都与最后一个条目完全相同。
我创建对象数组的方式有问题吗?
import java.util.Scanner;
public class Estudiante {
static String nombre = "", promedio = "", pueblo = "", entrada = "";
static int edad;
static Estudiante[] objArray = new Estudiante[4];
static Scanner input = new Scanner(System.in);
Estudiante(String nom, int ed, String prom, String pueb) {
this.nombre = nom;
this.edad = ed;
this.promedio = prom;
this.pueblo = pueb;
}
static void infoEst(String n) {
for (int i = 0; i < objArray.length; i++) {
if (objArray[i].nombre == n) {
System.out.println(objArray[i].nombre + ", " + objArray[i].pueblo);
}
}
}
public static void main(String[] args) {
objArray[0] = new Estudiante("Joshua", 20, "A", "Aguadilla");
objArray[1] = new Estudiante("Liliana", 21, "A", "Isabella");
objArray[2] = new Estudiante("Cano", 27, "C", "Aguadilla");
objArray[3] = new Estudiante("Eribelto", 22, "F", "Moca");
// 在执行时不会打印任何内容。
infoEst("Joshua");
infoEst("Liliana");
// if语句块在索引0上执行,因此会打印出 Eribelto, Moca 4 次。
infoEst("Eribelto");
// 所有这些都打印出 "Eribelto",即数组中的最后一个元素。
System.out.println(objArray[0].nombre);
System.out.println(objArray[1].nombre);
System.out.println(objArray[2].nombre);
System.out.println(objArray[3].nombre);
}
}
英文:
I'm trying to get the function infoEst() to iterate over an array of objects
and print out the object's name and location. It iterates fine but it seems like every
index in the array is the exact same as the last entry.
Am I creating the array of objects incorrectly?
import java.util.Scanner;
public class Estudiante {
static String nombre= "", promedio = "", pueblo = "", entrada = "";
static int edad;
static Estudiante[] objArray = new Estudiante[4];
static Scanner input = new Scanner(System.in);
Estudiante(String nom, int ed, String prom, String pueb) {
this.nombre = nom;
this.edad = ed;
this.promedio = prom;
this.pueblo = pueb;
}
static void infoEst(String n) {
for (int i = 0; i < objArray.length; i++) {
if (objArray[i].nombre == n) {
System.out.println(objArray[i].nombre + ", " + objArray[i].pueblo);
}
}
}
public static void main(String[] args) {
objArray[0] = new Estudiante("Joshua", 20, "A", "Aguadilla");
objArray[1] = new Estudiante("Liliana", 21, "A", "Isabella");
objArray[2] = new Estudiante("Cano", 27, "C", "Aguadilla");
objArray[3] = new Estudiante("Eribelto", 22, "F", "Moca");
// does not print anything when executed.
infoEst("Joshua");
infoEst("Liliana");
// the if statement block executes on index 0 so
// it prints Eribelto, Moca 4 times.
infoEst("Eribelto");
// All of these print "Eribelto" which is last in the array
System.out.println(objArray[0].nombre);
System.out.println(objArray[1].nombre);
System.out.println(objArray[2].nombre);
System.out.println(objArray[3].nombre);
}
}
答案1
得分: 1
迭代工作正常,但似乎数组中的每个索引与最后一个条目完全相同。
static String nombre = "", promedio = "", pueblo = "", entrada = "";
不要使用静态变量。这意味着类的每个实例共享相同的信息。
英文:
> It iterates fine but it seems like every index in the array is the exact same as the last entry.
static String nombre= "", promedio = "", pueblo = "", entrada = "";
Don't use static variables. This means each instance of the class shares the same information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论