在Java中删除元素后缩短数组长度 [英] Shorten array length once element is remove in Java

查看:118
本文介绍了在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.

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆