简体   繁体   English

为Java 2维动态数组增加价值

[英]add value to java 2 dimensional dynamic arrays

how can I possibly add a value to a 2 dimensional dynamic arrays? 如何为二维动态数组添加值?

String emp[][] = {
{"001","Superman","Manda","123-1233","02-14-1970","Male","Manager","20,000","Regular"},
{"002","Batman","Manda","33-1233","07-14-2007","Male","Clerk","20,000","Regular"},
};

I try this code 我尝试这段代码

int arrLen = emp.lenght;
emp[arrLen][0] = "003"
emp[arrLen][1] = "Superwoman"
emp[arrLen][2] = "QC"
emp[arrLen][3] = "123-1233"
emp[arrLen][4] = "03-12-2012"
emp[arrLen][5] = "Female"
emp[arrLen][6] = "Supervisor"
emp[arrLen][7] = "10,000"
emp[arrLen][8] = "Regular"

but it doesnt work 但它不起作用

can anyone help me? 谁能帮我?

thanks 谢谢

You can't add to an array - after creation, the size is fixed. 您无法添加到数组-创建后,大小是固定的。 You should consider using a List<E> implementation such as ArrayList ... ideally having created a type to encapsulate this information better than just an array of strings. 您应该考虑使用List<E>实现,例如ArrayList ...理想情况下,已经创建了一种类型来封装此信息,而不仅仅是字符串数组。 It looks like you've got dates, names, numeric values etc in there - why not use that information? 好像那里有日期,名称,数字值等-为什么不使用该信息?

For example: 例如:

List<Person> people = new ArrayList<Person>();
people.add(new Person(1, "Superman", "Manda", new TelephoneNumber("123-1233"),
           new LocalDate(1970, 14, 2), Gender.MALE,
           "Manager", 20000, SalaryType.REGULAR);

You have a typo 你有错字

lenght

Also, statements have to end in 另外,语句必须以

;

Finally, what your actual problem is: the length of an array is fixed at creation; 最后,您的实际问题是:数组的长度在创建时是固定的; array[array.length] will always point outside the array. array [array.length]将始终指向数组外部。

Java arrays have fixed length. Java数组具有固定长度。 If you want resizable array use ArrayList . 如果要调整数组大小,请使用ArrayList

Example: 例:

ArrayList<String[]> list = new ArrayList<String[]>();
list.add(new String[]{"Superman","Manda","..."});
list.add(new String[]{"Superwoman","Manda","..."});

array.length返回一个比索引高1的数字。

Array objects are of fixed size. 数组对象的大小固定。 You can allocate a new, larger String[][] object and move the subarrays over to it -- ie, 您可以分配一个更大的新String[][]对象,并将子数组移到该对象上,即

String[][] tmp = new String[emp.length + 1][];
for (int i=0; i<emp.length; ++i)
   tmp[i] = emp[i];
emp = tmp;

After this, there's an empty spot at the end of your main array. 此后,主阵列的末尾有一个空白点。 Fill it with 装满

emp[emp.length-1] = new String[] {"003", ...};

You have two big issues with your current code. 您当前的代码有两个大问题。

1) Arrays aren't dynamic, and have fixed size. 1)数组不是动态的,并且具有固定的大小。 You should instead use the ArrayLists data structure, where you can add values. 相反,您应该使用ArrayLists数据结构,您可以在其中添加值。

2) Your array.Length returns some length n, but your array is indexed from 0 to (n-1). 2)您的array.Length返回一些长度n,但是您的数组的索引是从0到(n-1)。 Hence, referencing to the length, will give you an index out of bounds exception. 因此,参考长度,将为您提供索引超出范围的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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