I'm trying to display the number of elements in an Array that I have made. It is a regular Array with a string as the data type. If it is possible to display the total number of elements in an Array, will the code be similar to an ArrayList's?
我正在尝试显示我所创建的数组中的元素数量。它是一个带有字符串作为数据类型的常规数组。如果可以显示数组中元素的总数,代码是否类似于ArrayList?
String[] list = new String[14];
list = new String[] {"Bob", "Sal", "Jimmy" ,"John"};
list = insert(list, "Joe", 0);
list = insert(list, "Manny", list.length);
list = insert(list, "Joey", list.length/2);
System.out.println(Arrays.toString(list));
}
public static String[] insert(String[] original, String value, int k) {
String[] copy = new String[original.length + 1];
for(int i=0; i<k; i++)
{
copy[i] = original[i];
}
copy[k] = value;
for(int i=k; i<original.length; i++)
{
copy[i+1] = original[i];
}
return copy;
}
2 个解决方案
#1
1
As already mentioned the best Array type to use is a List or ArrayList for inserting elements into that array by way of the ArrayList.add(index, valueToAdd) method. It of course can be done with basic Arrays but a little more work on your part is required to carry out the task as you have already noticed. One possible way might be like this:
如前所述,要使用的最佳Array类型是List或ArrayList,用于通过ArrayList.add(index,valueToAdd)方法将元素插入到该数组中。它当然可以使用基本的阵列来完成,但是你需要做一些工作来执行你已经注意到的任务。一种可能的方式可能是这样的:
public static String[] insertStringArrayElement(String[] original, String value, int atIndex) {
// Make sure the supplied index (k) is within bounds
if (atIndex < 0 || atIndex > original.length) {
throw new IllegalArgumentException("\ninsertArrayElement() Method Error! "
+ "The supplied index value is invalid ("
+ atIndex + ")!\n");
}
String[] tmp = new String[original.length + 1];
int elementCount = 0; //Counter to track original elements.
for(int i = 0; i < tmp.length; i++) {
// Are where we want to insert a new elemental value?
if (i == atIndex) {
tmp[i] = value; // Insert the new value.
i++; //Increment i so to add the orininal element.
// Make sure we don't go out of bounds.
if (i < original.length) {
tmp[i] = original[elementCount]; // Add original element.
}
}
// No we're not...
else {
tmp[i] = original[elementCount]; // Add original element.
}
elementCount++; // Increment counter for original element tracking.
}
return tmp; // Return our new Array.
}
As Andreas has so kindly pointed out, in your original posted code you had declared the list String Array and initialized it to contain 14 elements but in the very next line you reinitialize this array to contain only 4 elements. The first initialization is a waste of oxygen, time, memory, effort, etc since you could have just declared and initialized the Array like this:
正如安德烈亚斯如此友好地指出的那样,在原始发布的代码中,您已经声明了列表字符串数组并将其初始化为包含14个元素,但在下一行中,您重新初始化此数组以仅包含4个元素。第一次初始化是浪费氧气,时间,内存,努力等,因为您可能刚刚声明并初始化了数组,如下所示:
String[] list = new String[] {"Bob", "Sal", "Jimmy" ,"John"};
or better yet:
或者更好的是:
String[] list = {"Bob", "Sal", "Jimmy" ,"John"};
Just something to remember.
只是记住一些事情。
#2
0
I suggest you to use an ArrayList if possible, since it would be a lot easier with the method size()
. If you have to use an array, since the array is already sized, you could check with a for loop, which slot of the array is != to null and add them to a counter.
我建议你尽可能使用ArrayList,因为使用方法size()会更容易。如果必须使用数组,由于数组已经调整大小,您可以使用for循环进行检查,数组的哪个插槽是!= null并将它们添加到计数器中。
#1
1
As already mentioned the best Array type to use is a List or ArrayList for inserting elements into that array by way of the ArrayList.add(index, valueToAdd) method. It of course can be done with basic Arrays but a little more work on your part is required to carry out the task as you have already noticed. One possible way might be like this:
如前所述,要使用的最佳Array类型是List或ArrayList,用于通过ArrayList.add(index,valueToAdd)方法将元素插入到该数组中。它当然可以使用基本的阵列来完成,但是你需要做一些工作来执行你已经注意到的任务。一种可能的方式可能是这样的:
public static String[] insertStringArrayElement(String[] original, String value, int atIndex) {
// Make sure the supplied index (k) is within bounds
if (atIndex < 0 || atIndex > original.length) {
throw new IllegalArgumentException("\ninsertArrayElement() Method Error! "
+ "The supplied index value is invalid ("
+ atIndex + ")!\n");
}
String[] tmp = new String[original.length + 1];
int elementCount = 0; //Counter to track original elements.
for(int i = 0; i < tmp.length; i++) {
// Are where we want to insert a new elemental value?
if (i == atIndex) {
tmp[i] = value; // Insert the new value.
i++; //Increment i so to add the orininal element.
// Make sure we don't go out of bounds.
if (i < original.length) {
tmp[i] = original[elementCount]; // Add original element.
}
}
// No we're not...
else {
tmp[i] = original[elementCount]; // Add original element.
}
elementCount++; // Increment counter for original element tracking.
}
return tmp; // Return our new Array.
}
As Andreas has so kindly pointed out, in your original posted code you had declared the list String Array and initialized it to contain 14 elements but in the very next line you reinitialize this array to contain only 4 elements. The first initialization is a waste of oxygen, time, memory, effort, etc since you could have just declared and initialized the Array like this:
正如安德烈亚斯如此友好地指出的那样,在原始发布的代码中,您已经声明了列表字符串数组并将其初始化为包含14个元素,但在下一行中,您重新初始化此数组以仅包含4个元素。第一次初始化是浪费氧气,时间,内存,努力等,因为您可能刚刚声明并初始化了数组,如下所示:
String[] list = new String[] {"Bob", "Sal", "Jimmy" ,"John"};
or better yet:
或者更好的是:
String[] list = {"Bob", "Sal", "Jimmy" ,"John"};
Just something to remember.
只是记住一些事情。
#2
0
I suggest you to use an ArrayList if possible, since it would be a lot easier with the method size()
. If you have to use an array, since the array is already sized, you could check with a for loop, which slot of the array is != to null and add them to a counter.
我建议你尽可能使用ArrayList,因为使用方法size()会更容易。如果必须使用数组,由于数组已经调整大小,您可以使用for循环进行检查,数组的哪个插槽是!= null并将它们添加到计数器中。