0

i have declare an array and if i want to give some details like name, value to the element, what should i put in ?

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;

}

i make it to a function so that i can call it when necessary. and inside although inside the array only got 1 element, but the element will have name,id,value.

how to do it ? sorry, i am noob in programming.

Eric
  • 401
  • 4
  • 13
  • 21

4 Answers4

3

This is not array declaration int client = new int[1];. array should be declare with specific type like ClientDetail[] clDetails= new ClientDetail[10];, Here ClientDetail is custom type. Where ClientDetail type defination would be -

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

And you can create client details like -

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;
  }
}
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
3

An array will not hold multiple fields. An array is a collection of objects (or primitives). It seems like you want an array of an object that has the fields name, id and value.

First create your object

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;
  };

}

**Once you have created an object you can create an array of that object.

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

Once the array is created you can then work with the array of objects

for(Element element:elements){
   System.out.println(element.getName());
   System.out.println(element.getId());
   System.out.println(element.getValue());
}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
2

Create an object class with each instance of that class having the desired attributes with getters and setters. Put these in an array of the type of your object class. For my following code, it would be 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;
    }

}
EvenLisle
  • 4,672
  • 3
  • 24
  • 47
1

The way I will do this is to create a Class Client

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;
  }


}

Then use this class to store information

    :
    :
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");
      :
      :

}
    :
    :

You can check on ArrayIndexOutOfBoundsException when using the ArrayList's iterator to complete this code.

Community
  • 1
  • 1
Dexter Cato
  • 1,091
  • 2
  • 10
  • 11