繁体   English   中英

如何在ArrayList中打印对象的名称?

[英]How can I print the name of an object in an ArrayList?

我已经编写了以下代码:

import java.util.ArrayList;
import java.util.List;


public class TestBox {

    public static void main(String[]args){

        ShoeBox nike = new ShoeBox();
        Box present = new CandyBox(3,2,6);
        JewelryBox gift = new JewelryBox();
        Box pictures = new ShoeBox();
        Box skittles=new CandyBox(6,3,1);
        CandyBox dots=new CandyBox(3,2,1);
        Box jareds=new JewelryBox();
        List<Box> boxes=new ArrayList<Box>();
        boxes.add(nike);
        boxes.add(present);
        boxes.add(gift);
        boxes.add(pictures);
        boxes.add(skittles);
        boxes.add(dots);
        boxes.add(jareds);

        double temp=0;
        for (Box x:boxes)
            temp=temp+x.getVolume();

        for (int i=0;i<boxes.size();i++)
            System.out.println(boxes.get(i));

        double count=0;
        for (int k=0;k<boxes.size();k++)
            if ((boxes.get(0).getVolume())<(boxes.get(k).getVolume()))
                count=boxes.get(k).getVolume();
        System.out.println("The box with the biggest volume is the "+boxes.get((int)count)+".  The dimensions of the box"
                + "are "+boxes.get((int)count).getLength()+" x "+boxes.get((int)count).getWidth()+" x "
                +boxes.get((int)count).getHeight()+".");
    }
}

这是我的Box课程的测试人员。 子类显示在它们的创建中。 打印出吃喝玩乐或罐子之类的对象名称所需的代码行是什么?

我需要打印体积最大的对象的“名称”。 假设对象“ nike”的体积最大。 主机底部的打印声明应显示“最大的盒子是耐克。尺寸为12 x 12 x 12”。

看起来您想打印满足某些条件并存储在您的集合(您的List )中的变量的名称。 在Java中是不可能的,因为在运行时您无法访问变量的名称。 在这种情况下,您应该在类中添加一个字段,以帮助您标识正在使用的对象实例。 该字段可以是int idString name (为简单起见)。

为了简化操作,请在Box类中添加以下字段:

public class Box {
    //fields already declared here...
    //adding name field
    protected String name;
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

然后,在创建对象时填写此字段:

//setting the name of the object just with the name of the variable
//just for understanding purposes
//you cannot access to the name of the variable at runtime, no matter what
//also, you need to change the constructor of each class accordingly
ShoeBox nike = new ShoeBox("nike");
Box present = new CandyBox("present",3,2,6);
JewelryBox gift = new JewelryBox("gift");
Box pictures = new ShoeBox("pictures");
Box skittles=new CandyBox("skittles",6,3,1);
CandyBox dots=new CandyBox("dots",3,2,1);
Box jareds=new JewelryBox("jareds");

然后,当一个对象与您的条件匹配时,您可以将其用于所需/需要的对象,例如

  • 评判标准:最大数量
  • 怎么做:向用户显示其名称

码:

double temp = 0;
Box boxWithLargestVolume = null;
for (Box box : boxes) {
    if (temp < x.getVolume()) {
        temp = x.getVolume();
        boxWithLargestVolume = box;
    }
}
if (boxWithLargestVolume != null) {
    System.out.println("The box with the largest volume is: "
        + boxWithLargestVolume.getName());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM