对象信息与对象数组的索引不一致

huangapple go评论67阅读模式
英文:

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= &quot;&quot;, promedio = &quot;&quot;, pueblo = &quot;&quot;, entrada = &quot;&quot;;
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 &lt; objArray.length; i++) {
if (objArray[i].nombre == n) {
System.out.println(objArray[i].nombre + &quot;, &quot; + objArray[i].pueblo);
}
}
}   
public static void main(String[] args) {
objArray[0] = new Estudiante(&quot;Joshua&quot;, 20, &quot;A&quot;, &quot;Aguadilla&quot;);
objArray[1] = new Estudiante(&quot;Liliana&quot;, 21, &quot;A&quot;, &quot;Isabella&quot;);
objArray[2] = new Estudiante(&quot;Cano&quot;, 27, &quot;C&quot;, &quot;Aguadilla&quot;);
objArray[3] = new Estudiante(&quot;Eribelto&quot;, 22, &quot;F&quot;, &quot;Moca&quot;);
// does not print anything when executed.    
infoEst(&quot;Joshua&quot;); 
infoEst(&quot;Liliana&quot;);
// the if statement block executes on index 0 so
// it prints Eribelto, Moca 4 times.
infoEst(&quot;Eribelto&quot;);
// All of these print &quot;Eribelto&quot; 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= &quot;&quot;, promedio = &quot;&quot;, pueblo = &quot;&quot;, entrada = &quot;&quot;;

Don't use static variables. This means each instance of the class shares the same information.

huangapple
  • 本文由 发表于 2020年9月13日 23:32:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63872497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定