Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在ArrayList中打印对象的名称?_Java_Arraylist - Fatal编程技术网

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

Java 如何在ArrayList中打印对象的名称?,java,arraylist,Java,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 Jewelr

我已经编写了以下代码:

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()+".");
    }
}
import java.util.ArrayList;
导入java.util.List;
公共类测试盒{
公共静态void main(字符串[]args){
鞋盒nike=新鞋盒();
盒子当前=新的烛台(3,2,6);
珠宝盒礼品=新珠宝盒();
盒子图片=新鞋盒();
盒形天窗=新的烛台(6,3,1);
烛台点=新烛台(3,2,1);
Box jareds=新的珠宝盒();
列表框=新的ArrayList();
框。添加(nike);
方框。添加(当前);
盒子。添加(礼物);
框。添加(图片);
框。添加(小煤斗);
框。添加(点);
框。添加(jareds);
双温=0;
用于(方框x:方框)
temp=temp+x.getVolume();

for(int i=0;i看起来您希望打印满足特定条件并存储在集合中的变量的名称(您的
列表
)。这在Java中是不可能的,因为在运行时您无法访问变量的名称。在这种情况下,您应该在类中添加一个字段,以帮助您识别正在使用的对象实例。此字段可以是
int id
字符串名
(保持简单)

为了简化操作,请在
框中添加此字段
类:

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;
体积最大的长方体=空;
用于(盒子:盒子){
如果(温度
对于对象的名称,您的意思是如何获取类的简单名称?如果是这样,请使用
yourObject.getClass().getSimpleName()
@LuiggiMendoza你能解释什么是简单名称吗?很抱歉,这是我第一年的学习代码。我正在寻找一种方法,在ArrayList框中打印出对象的名称。对象没有名称。类有名称。类有全名,即它所属包的名称和对象的简单名称类。例如,如果您的
TestBox
类位于包
edu.yourcollege.noah
,则其全名将为
edu.yourcollege.noah.TestBox
,简单名称将为
TestBox
@luigimendoza我不想打印类名。我需要打印“名称”体积最大的对象。假设对象“nike”的体积最大。主视图底部的打印语句应显示“体积最大的盒子是nike。尺寸为12 x 12 x 12。”同样,对象没有名称。在这种情况下,您可以确定哪个对象符合您的条件(在本例中,是体积最大的对象),将其存储在一个临时变量中,然后根据需要使用它。问题是,这项作业是由我的老师分配的。她给了我们构造函数的参数,并给了我们测试仪的客户端代码,其中包括框的实例化。@NoahBarboza告诉你的教授,如果你没有fie如果你想识别你的物体,那么就不会有任何人类的方法来识别它。这一切都在软件中(在我们的头脑中)。
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());
}