[英]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?
例如,如何为元素1分配名称和描述?
Object[] myArray = new Object7; Object [] myArray = new Object7; Object myObject = myArray[5];
对象myObject = myArray [5]; // get object at index 5 then make changes to your object
//在索引5处获取对象,然后对您的对象进行更改
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]
. 要获取元素,只需使用
arrayName[index]
。 Arrays are 0-based
, so getting the 5th element is done by someNumbers[4]
. 数组是从
0-based
,因此第5个元素的获取是由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). 附加说明:如果尝试访问数组外部的元素(使用负索引或大于或等于数组长度的索引),则将获得
ArrayIndexOutOfBoundsException
。
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?
如何为元素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. 请注意,元素1是数组的第二个元素,因为indes以0开头。
For example your object
here is of a class
type Car
. 例如,这里的
object
是Car
类型的class
。 And your Car object
has a method called setNumberPlate(String x)
which is something like : 并且您的
Car object
具有一个名为setNumberPlate(String x)
的方法,该方法类似于:
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
. 你需要有一个方法的原因是因为它是一个很好的做法,让您的数据成员
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. 如果将对象存储在
array
,那么这就是您要做的。
Car[] arr = new Car[10];
arr[0].setNumberPlate("UIA9490");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.