简体   繁体   中英

How to edit elements in array of class type in java?

How can I edit a single element in array of a class type. For example how can I assign a name and description for element 1?

Object[] myArray = new Object7; Object myObject = myArray[5]; // get object at index 5 then make changes to your object

Create the array:

type[] arrayName = new type[size];

Example:

int[] someNumbers = new int[10]; // array with 10 slots

To get an element, simply use arrayName[index] . Arrays are 0-based , so getting the 5th element is done by someNumbers[4] .

Let's say we have an array of Persons and you want to change a person address.

Person personObj = arrayOfPersons[3]; // get the 4th person
personObj.setAddress("New York");

That's it! You're done!

Additional notes: You will get an ArrayIndexOutOfBoundsException if you try to access an element outside the array(using a negative index or an index greater or equal that the array length).

Do you mean to add extra properties to this class?

I suppose you have an array

String[] data = new String[]{"a", "b", "c"}

To add extra properties, may be you can try to create a wrapper:

class Wrapper<T> {

    private T origin

    public String name;
    public String desc

    public Wrapper(T data) {
        origin = data;
    }
}

To add extra properties:

Map<String, Wrapper> extraProperties = new HashMap<String, Wrapper>();

Wrapper<String> w = new Wrapper<String>(data[1]);
w.name = "a name";
w.desc = "description"

extraProperties.put(data[1], w);

To access the extra properties:

if(extraProperties.contains(data[1])) {
    extraProperties.get(data[1]).name
    extraProperties.get(data[1]).desc
}

how can I assign a name and description for element 1?

That depends on your class, but here's a (bad) example:

YourClass[] array = .... ;           // wherever it comes from
array[1].name = "World";             // if name is a member of your class
array[1].setDescription("whatever"); // if there is a setter for description

Note that element 1 is the second element of the array as indeces start with 0.

For example your object here is of a class type Car . And your Car object has a method called setNumberPlate(String x) which is something like :

void setNumberPlate (String str)
{
   //numberPlate is a String data member of your Car class
   numberPlate = str;
}

The reason you need to have a method is because its a good practice to keep the data members of your class private . So, for example, here's the class :

Class Car
{
   private String numberPlate;

   public void setNumberPlate(String str)
   {
      numberPlate = str;
   }

}

Then you can access this method in another class. If you store your objects in an array , then this is what you do.

Car[] arr = new Car[10];

arr[0].setNumberPlate("UIA9490");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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