在Java中删除元素后缩短数组长度 [英] Shorten array length once element is remove in Java
问题描述
注意:
以下是我的作业/作业,如果你愿意,请随时不回答。
Note: Following is my homework/assignment, feel free not to answer if you will.
我想删除/删除元素字符串数组(设置)基本,我不允许使用Collections..etc。
I want to delete/remove an element from an String array(Set) basic, I'm not allowed to use Collections..etc.
现在我有:
void remove(String newValue) {
for ( int i = 0; i < setElements.length; i++) {
if ( setElements[i] == newValue ) {
setElements[i] = "";
}
}
}
我做我想要的,因为它从数组中删除元素,但它没有缩短长度。以下是输出,基本上它删除了索引#1的元素。
I does what I want as it remove the element from an array but it doesn't shorten the length. The following is the output, basically it remove the element indexed #1.
D:\javaprojects>java SetsDemo
Enter string element to be added
A
You entered A
Set size is: 5
Member elements on index: 0 A
Member elements on index: 1 b
Member elements on index: 2 hello
Member elements on index: 3 world
Member elements on index: 4 six
Set size is: 5
Member elements on index: 0 A
Member elements on index: 1
Member elements on index: 2 hello
Member elements on index: 3 world
Member elements on index: 4 six
推荐答案
你不能改变长度一个数组对象一旦创建。以下是 JLS 10.2的摘录。数组变量:
You can't change the length of an array object once it's created. Here's an excerpt from JLS 10.2. Array Variables:
创建数组对象后,其长度永远不会改变。要使数组变量引用不同长度的数组,必须为变量分配对不同数组的引用。
Once an array object is created, its length never changes. To make an array variable refer to an array of different length, a reference to a different array must be assigned to the variable.
这意味着对于这个问题,你必须分配一个比原始数组短的一个元素的新数组,并复制剩余的元素。
This means that for this problem, you'd have to allocate a new array that's one-element shorter than the original array, and copy over the remaining elements.
如果你需要删除索引 k
的元素,原始数组有 L
元素,然后你需要复制元素(上层)边界是独家的):
If you need to remove element at index k
, and the original array has L
elements, then you need to copy over elements (upper bounds are exclusive):
- 从
[0,k)
到[0,k)
(k
元素) - 来自
[k + 1,L)
至[k,L-1)
(Lk-1
元素)。 - 总计
L-1
复制的元素
- From
[0,k)
to[0,k)
(k
elements) - From
[k+1,L)
to[k,L-1)
(L-k-1
elements). - For a total of
L-1
elements copied
static String[] removeAt(int k, String[] arr) {
final int L = arr.length;
String[] ret = new String[L - 1];
System.arraycopy(arr, 0, ret, 0, k);
System.arraycopy(arr, k + 1, ret, k, L - k - 1);
return ret;
}
static void print(String[] arr) {
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
String[] arr = { "a", "b", "c", "d", "e" };
print(arr); // prints "[a, b, c, d, e]"
arr = removeAt(0, arr);
print(arr); // prints "[b, c, d, e]"
arr = removeAt(3, arr);
print(arr); // prints "[b, c, d]"
arr = removeAt(1, arr);
print(arr); // prints "[b, d]"
arr = removeAt(0, arr);
arr = removeAt(0, arr);
print(arr); // prints "[]"
}
这使用 System.arraycopy
;如果不允许,你总是可以写自己的。
This uses System.arraycopy
; you can always write your own if this isn't allowed.
static void arraycopy(String[] src, int from, String[] dst, int to, int L) {
for (int i = 0; i < L; i++) {
dst[to + i] = src[from + i];
}
}
这是一个简单的实现,无法处理 src == dst
,但在这种情况下就够了。
This is a simplistic implementation that doesn't handle src == dst
, but it's sufficient in this case.
- 在java中删除数组中的元素可以将它设置为null吗?
- 答案:NO!
大多数时间,使用
==
比较字符串
对象是错误的。您应该使用等于
。Most of the time, using
==
to compareString
objects is a mistake. You should useequals
instead.String ha1 = new String("ha"); String ha2 = new String("ha"); System.out.println(ha1 == ha2); // prints "false" System.out.println(ha1.equals(ha2)); // prints "true"
参见
- Java String.equals vs. ==
- 等于和==之间的差异
- 为什么我们有==运算符时的equals()方法?
- Java String.equals versus ==
- Difference Between Equals and ==
- why equals() method when we have == operator?
See also
这篇关于在Java中删除元素后缩短数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!