Java 如何打印对象类的数组索引?

Java 如何打印对象类的数组索引?,java,Java,我有一个比较大的代码,我要打印一个对象类中数组的槽号,这个对象类的结果是权重最高的 我主要是这样声明我的数组: Car x[] = new car[2]; 现在,当我试着找出哪辆车的重量最高时,我已经在for循环中进行了验证 //.getweight obtains the weight of each car if(x[i].getWeight() > max){ max = x[i].getWeight(); } 该代码给出了最重汽车的实际重量编号,但我要打印的是实际的

我有一个比较大的代码,我要打印一个对象类中数组的槽号,这个对象类的结果是权重最高的

我主要是这样声明我的数组:

Car x[] = new car[2];
现在,当我试着找出哪辆车的重量最高时,我已经在for循环中进行了验证

//.getweight obtains the weight of each car

if(x[i].getWeight() > max){

  max = x[i].getWeight();

 }
该代码给出了最重汽车的实际重量编号,但我要打印的是实际的汽车编号。比较工作,但我找不到一种方法,只是打印插槽,对应于最重的汽车

使用完整代码更新:

我的目标车:

public class Car
{

private String color;
private int weight;
private int state;
private int fuel;
private int Maxspeed ;
private int engine;



public Car(){
 this.color = "White";
 this.weight = 1000;
 this.state = 0;
 this.fuel =0;
 this.Maxspeed = 0;
 this.engine = 0;
}

public Car( String color, int weight,
            int state, int fuel, int Maxspeed 
            ){
   this.color = color;
   this.weight = weight;
   this.state = state;
   this.fuel = fuel;
   this.Maxspeed = Maxspeed;
}




public String getColor(){
  return this.color;
}

public int getweight(){
  return this.weight;
} 

public int getstate(){

   return this.state;
}

public int getfuel(){
  return this.fuel;
}

public int getMaxspeed(){
  return this.Maxspeed;
}

public int getengine(){
    return this.engine;
}


public void setColor( String color ){
  this.color = color;

}

public void setweight( int weight ){
   this.weight = weight;
}

public void setstate( int state ){

   this.state = state;

}

public void setfuel( int fuel ){
  this.fuel = fuel;
}

public void setMaxspeed( int Maxspeed ){
  this.Maxspeed = Maxspeed;
}

public void setengine(int engine){
   this.engine = engine;
}




public void showdata(){
  System.out.println( "\nCar's color is: " + this.getColor() );
  System.out.println( "Car's weight is: " + this.getweight() );
  System.out.println( "State: " + this.getstate() );
  System.out.println( "Fuel: " + this.getfuel());
  System.out.println( "Max speed: "  + this.getMaxspeed());


}

public void accelerate( int speed ){
  if( this.getstate() == 0 || 
      this.getstate() == 3 || 
      this.getstate() == 4 || 
      this.getMaxspeed() < speed )
   {
    System.out.println("\nCar cannot accelerate...");
   }
   else{
      System.out.println("\nCar is accelerating...");
      this.setfuel(this.getfuel()-2);
      this.setstate(2);
        if( this.getfuel() <= 0 ){
           this.setstate(4);
        }
    }


}

   public void crash(){
      this.setstate(3);
      System.out.println("\nCrash!!!");
    }
   public void stop(){
      this.setstate(1);
      System.out.println("\nCar has stopped.");
    }

   public void addfuel(int fuel){

       if(this.getstate() == 0 || this.getstate() == 4){
           this.setfuel(this.getfuel()+ fuel);
       }
       else{
           System.out.println("You can't add fuel.");
        }


    }

   public void repair(){

       if(this.getstate() == 3){
           this.setstate(1);
           System.out.println("The car has been repaired");
        }
       else{
           System.out.println("The car is not broken");
        } 
    }


}
公车
{
私有字符串颜色;
私有整数权重;
私人和国家;
私人燃料;
私有int-Maxspeed;
私人内燃机;
公共汽车{
this.color=“白色”;
这个重量=1000;
该状态=0;
该燃料=0;
此参数为.Maxspeed=0;
这个引擎=0;
}
公共汽车(字符串颜色、整数重量、,
int状态,int燃料,int最大速度
){
这个颜色=颜色;
重量=重量;
this.state=状态;
这个。燃料=燃料;
这个.Maxspeed=Maxspeed;
}
公共字符串getColor(){
返回此.color;
}
公共整数getweight(){
返回此值。重量;
} 
public int getstate(){
返回此.state;
}
public int getfuel(){
还这个。燃料;
}
public int getMaxspeed(){
返回这个.Maxspeed;
}
公共int getengine(){
归还这台发动机;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
公共空隙设定重量(内部重量){
重量=重量;
}
公共无效设置状态(int状态){
this.state=状态;
}
公共燃料(内部燃料){
这个。燃料=燃料;
}
公共无效setMaxspeed(int Maxspeed){
这个.Maxspeed=Maxspeed;
}
公共无效设置引擎(内部引擎){
这个。发动机=发动机;
}
公共void showdata(){
System.out.println(“\nCar的颜色是:”+this.getColor());
System.out.println(“汽车的重量是:“+this.getweight());
System.out.println(“状态:+this.getstate());
System.out.println(“Fuel:+this.getfuel());
System.out.println(“最大速度:+this.getMaxspeed());
}
公共空间加速(整数速度){
如果(this.getstate()==0 | |
this.getstate()==3 | |
this.getstate()==4 | |
此参数为.getMaxspeed()

现在,当我编译和测试哪个引擎或汽车更小或最重时,我得到的结果是数字1

数组索引将是
i


实际的汽车是
x[i]

您需要保留对最重汽车索引的引用。在这个例子中,我使用的是maxIndex

float maxWeight = 0;
int maxIndex = 0;
for (int i = 0; i < x.length; i++) { 
   float weight = x[i].getWeight();
   if (weight > max) {
       maxWeight = weight;
       maxIndex = i;
   }
}

System.out.println("Slot of heaviest car: " + maxIndex);
float maxWeight=0;
int maxIndex=0;
对于(inti=0;i最大值){
最大重量=重量;
maxIndex=i;
}
}
System.out.println(“最重车辆的槽:+max
float maxWeight = 0;
int maxIndex = 0;
for (int i = 0; i < x.length; i++) { 
   float weight = x[i].getWeight();
   if (weight > max) {
       maxWeight = weight;
       maxIndex = i;
   }
}

System.out.println("Slot of heaviest car: " + maxIndex);