2014-11-23 75 views
0

我有这样的代码已经写:如何在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类。子类被显示在它们的创作中。打印对象名称所需的代码行是什么?例如吃喝玩乐或jareds?

我需要打印体积最大的对象的“名称”。假设对象“耐克”拥有最大的音量。主要底部的印刷说明应该说:“体积最大的盒子是耐克,尺寸是12×12×12”。

+1

带*的对象的名称*你的意思如何获得该类的简单名称?如果是这样,那么使用'yourObject.getClass()。getSimpleName()'。 – 2014-11-23 02:17:14

+0

@LuiggiMendoza你能解释一个简单的名字是什么吗?对不起,这是我第一年的学习代码。我正在寻找一种方法来打印ArrayList框中的对象的名称。 – 2014-11-23 02:19:31

+2

好吧,一个对象没有名字。一个班有一个名字。一个类有一个全名,它是它所属的包的名称和类的简单名称。例如,如果你的'TestBox'类在'edu.yourcollege.noah'包内,那么它的全名是'edu.yourcollege.noah.TestBox',简单的名字就是'TestBox'。 – 2014-11-23 02:22:31

回答

0

看起来您想要打印符合特定条件并存储在您的收藏(您的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()); 
} 
+0

问题在于这个任务是由我的老师分配的。她给了我们构造函数的参数,她给了我们测试器的客户端代码,其中包括实例化框。 – 2014-11-23 02:44:56

+0

@NoahBarboza告诉你的教授,如果你没有一个领域来识别你的对象,那么将不会有任何人类的方式来识别它。这一切都在软件中(并在我们的脑海中)。 – 2014-11-23 02:46:39