尝试删除数组中的元素而不使用任何导入方法

时间:2015-02-11 23:40:19

标签: java

已经尝试了很长时间但没有运气。到目前为止我的代码:(留下一些,因此更容易阅读)。

public class MyArrayList {
public String[] arrays = {};

public MyArrayList() {
    arrays = new String[5];

    }
public String get(int i) {
    return arrays[i];
}

    public boolean remove(String element) {
    String[] result = arrays;
    if (element != null) {
        int indexOf = -1;
        for (int index = 0; index < arrays.length; index++) {
            if (arrays[index].equals(element)) {
                indexOf = index;
                break;
            }
        }
        if (indexOf > -1) {
            result = new String[arrays.length -1];
            for(int index = 0; index < indexOf; index++){
                result[index]=arrays[index];
            }
            for (int index = indexOf + 1; index < arrays.length; index++) {
                result[index -1]=arrays[index];
                arrays = result;
                return true;
            }

        }
    }
    return false;
}

public class MyArrayListTest {

static MyArrayList zoo = new MyArrayList();
System.out.print("The zoo now holds " + zoo.size() + " animals: ");
    for (int j = 0; j < zoo.size(); j++) System.out.print(zoo.get(j) + " ");
    System.out.println();
}


public static void main(String[] args) {

    System.out.println("Testing constructor, add(object) and size() ");
    zoo.add("Ant");
    zoo.add("Bison");
    zoo.add("Camel");
    zoo.add("Dog");
    zoo.add("Elephant");
    zoo.add("Frog");
    zoo.add("Giraffe");
    zoo.add("Horse");
    printZoo();
    System.out.println();

System.out.println("Testing remove(object) ");
    System.out.println("Elephant was " + ((zoo.remove("Elephant"))? "removed " : "not in zoo "));
    System.out.println("Zebra was " + ((zoo.remove("Zebra"))? "removed " : "not in zoo "));
    System.out.println("Horse was " + ((zoo.remove("Horse")) ? "removed " : "not in zoo "));
    System.out.println("Aardvark was " + ((zoo.remove("Aardvark"))? "removed " : "not in zoo "));
    printZoo();
    System.out.println();

现在代码打印:“Aardvark Ant Antelope Bison Camel Dog Elephant Frog Giraffe Gorilla Horse Ibex”

它应该删除Aardvark,Horse和Elephant,并且应该说“斑马不在动物园”。所以在remove方法中这将是错误的。

谢谢,所有帮助表示感谢,因为我是一名非常优秀的Java程序员。

还需要在特定索引处删除的额外位。但是,如果我做对了,那将有助于我。

1 个答案:

答案 0 :(得分:0)

因此,您希望缩小数组,以便没有空插槽。

您要做的第一项任务是确定数值是否实际存在于数组中(indexOf值)

if (element!= null) {
    int indexOf = -1;
    for (int index = 0; index < arrays.length; index++) {
        if (arrays[index].equals(element)) {
            indexOf = index;
            break;
        }
    }

这有两件事,它告诉数组中的值是否存在以及它存在的位置,这很有用......

有了这些信息,您可以创建新阵列并开始将内容移入其中

if (indexOf > -1) {
    String newArray[] = new String[arrays.length - 1];
    for (int index = 0; index < indexOf; index++) {
        newArray[index] = arrays[index];
    }
    for (int index = indexOf + 1; index < arrays.length; index++) {
        newArray[index - 1] = arrays[index];
    }
    arrays = newArray;
}

所有这一切都是将arrays的内容复制到newArray,跳过indexOf处的元素,然后将结果分配回`arrays

既然你无法提供一个可运行的例子,我必须自己做...

import java.util.Arrays;

public class Test {

    public static void main(String[] args) {
        String[] data = new String[]{
            "Ant",
            "Bison",
            "Camel",
            "Dog",
            "Elephant",
            "Frog",
            "Giraffe",
            "Horse",};

        System.out.println(Arrays.toString(data));

        data = removeFrom("Dog", data);
        System.out.println(Arrays.toString(data));
        data = removeFrom("Ant", data);
        System.out.println(Arrays.toString(data));
        data = removeFrom("Horse", data);
        System.out.println(Arrays.toString(data));

    }

    protected static String[] removeFrom(String element, String[] arrays) {

        String[] result = arrays;
        if (element != null) {

            // You should first determine if the value exists in the incoming array...
            int indexOf = -1;
            for (int index = 0; index < arrays.length; index++) {
                if (arrays[index].equals(element)) {
                    indexOf = index;
                    break;
                }
            }

            if (indexOf > -1) {

                result = new String[arrays.length - 1];
                for (int index = 0; index < indexOf; index++) {
                    result[index] = arrays[index];
                }
                for (int index = indexOf + 1; index < arrays.length; index++) {
                    result[index - 1] = arrays[index];
                }

            }

        }
        return result;

    }
}

这将输出

[Ant, Bison, Camel, Dog, Elephant, Frog, Giraffe, Horse]
[Ant, Bison, Camel, Elephant, Frog, Giraffe, Horse]
[Bison, Camel, Elephant, Frog, Giraffe, Horse]
[Bison, Camel, Elephant, Frog, Giraffe]