【问题标题】:How to get details for the element in an array of java?如何获取java数组中元素的详细信息?
【发布时间】:2012-10-27 09:28:31
【问题描述】:

我已经声明了一个数组,如果我想给出一些细节,比如元素的名称、值,我应该输入什么?

import java.awt.*;

public class createNode {
private int id;
private String name;
private String value;

ClientDetail[] clDetails= new ClientDetail[1]

c1Details.id = client1;
c1Details.name = client1;

}

我把它做成一个函数,以便我可以在必要时调用它。里面虽然数组里面只有1个元素,但是元素会有名字,id,值。

怎么做?对不起,我是编程菜鸟。

【问题讨论】:

  • 将一个对象放入具有这些属性的数组中。可能是自定义的 ClientClientDetails 对象。
  • 该代码会编译吗?我怀疑。
  • 这是什么:-private createNode{};??而且你的import java.awt.* 在这里并不是真正需要的。

标签: java arrays


【解决方案1】:

这不是数组声明int client = new int[1];。数组应该声明为特定类型,如ClientDetail[] clDetails= new ClientDetail[10];,这里ClientDetail 是自定义类型。 ClientDetail 类型定义在哪里 -

public class ClientDetail{
   private int id;
   private String name;
   private int value;
   <setter and getter method>
}

您可以创建客户详细信息,例如 -

public ClientDetail[] createClientArray(){      
  Scanner in = new Scanner(System.in);
  System.out.print("Enter Number of Client -");
  int number= in.nextInt();
  ClientDetail[] clDetails= new ClientDetail[number];  
  for(int i=0;i<number;i++){
    ClientDetail cl = new ClientDetail();
    System.out.print("Enter Name -");
    cl.setName(in.next());
    System.out.print("Enter ID -");
    cl.setId(in.nextInt());
    System.out.print("Enter Value -");
    cl.setId(in.nextInt());
    clDetails[i] = cl;
  }
}

【讨论】:

    【解决方案2】:

    创建一个对象类,该类的每个实例都具有所需的 getter 和 setter 属性。将它们放入对象类类型的数组中。对于我的以下代码,它将是 DummyClass[] array = new DummyClass[desiredLength];

    public class DummyClass{
    
        private String attribute1;
        private int attribute2;
    
        public DummyClass(String attribute1, int attribute2){
            this.attribute1 = attribute1;
            this.attribute2 = attribute2;
        }
    
        public int getAttribute1(){
            return attribute1;
        }
    
        public int getAttribute2(){
            return attribute2;
        }
    
        public void setAttribute1(String attribute1){
            this.attribute1 = attribute1;
        }
    
        public void setAttribute1(int attribute2){
            this.attribute2 = attribute2;
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      一个数组不会包含多个字段。数组是对象(或原语)的集合。似乎您想要一个包含字段名称、id 和值的对象数组。

      首先创建您的对象

      public class Element(){
      
        private String name;
        private String id;
        private String value;
      
        public Element(String name, String id, String value){
          this.name = name;
          this.id = id;
          this.value = value;
        }
      
        public String getName(){
          return this.name;
        }
      
        public String getId(){
          return this.id;
        }
      
        public String getValue(){
          return this.value;
        };
      
      }
      

      **一旦你创建了一个对象,你就可以创建一个该对象的数组。

      Element[] elements = {new Element("first", "1", "Value1"), new Element("second", "2", "Value2") };
      

      创建数组后,您就可以使用对象数组了

      for(Element element:elements){
         System.out.println(element.getName());
         System.out.println(element.getId());
         System.out.println(element.getValue());
      }
      

      【讨论】:

      • 感谢您的建议。我想我可以理解如何添加细节。非常感谢..
      • 没问题,很高兴能帮上忙。您可能希望在该对象上放置 setter 方法。
      【解决方案4】:

      我将这样做的方式是创建一个类客户端

      public class Client {
      
        //declare some private variables
        private String name;
        private int id;
        private String value;
      
        // create getters and setters for the parameters
        public void setName(String name) {
          this.name = name;
        }
        public String getName() {
          return this.name;
        }
        public void setId(int id) {
          this.id = id;
        }
        public int getId() {
          return this.id;
        }
        public void setValue(String value) {
          this.value = value;
        }
        public String getValue() {
          return this.value;
        }
      
      
      }
      

      然后使用这个类来存储信息

          :
          :
      public void createClientArray()
      {
        // Create an ArrayList of Client
        ArrayList<Client> clients = new ArrayList<Client>();
      
        // Instantiate Client class to store the information
        Client clientObject = new Client();
        clientObject.setName("Client Name");
        clientObject.setId(1);
        clientObject.setValue("Any Value");
      
        // Add to the ArrayList
        clients.add(clientObject);
      
        // Store another client's information
        Client clientObject2 = new Client();
        clientObject2.setName("Client Name 2");
        clientObject2.setId(2);
        clientObject2.setValue("Any Value 2");
            :
            :
      
      }
          :
          :
      

      您可以查看ArrayIndexOutOfBoundsException when using the ArrayList's iterator 以完成此代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-06
        • 1970-01-01
        • 2017-09-24
        • 1970-01-01
        • 1970-01-01
        • 2021-11-30
        • 1970-01-01
        相关资源
        最近更新 更多