Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否从数组中删除元素并使用嵌套循环移动其余元素?_Java_Arrays_Loops_Nested - Fatal编程技术网

Java 是否从数组中删除元素并使用嵌套循环移动其余元素?

Java 是否从数组中删除元素并使用嵌套循环移动其余元素?,java,arrays,loops,nested,Java,Arrays,Loops,Nested,这是我目前的代码: public class CustomerListerArray { public static void main(String[] args) { // TODO Auto-generated method stub //creating the array String[] customerName = new String [7]; customerName[0] = "Beth"; customerName[1] =

这是我目前的代码:

public class CustomerListerArray {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    //creating the array 
    String[] customerName = new String [7]; 
    customerName[0] = "Beth"; 
    customerName[1] = "Jerry";
    customerName[2] = "Rick"; 
    customerName[3] = "Summer";
    customerName[4] = "Morty";

    // first loop/test
    for(String x : customerName) { 
        System.out.println(x);
    }
    System.out.println(" ");

    //second loop/test
    customerName[5] = customerName[3];
    customerName[6] = customerName[4];
    customerName[3] = "Rick";
    customerName[4] = "Jessica";

    for(String x : customerName) { 
        System.out.println(x);
    }
    System.out.println(" ");

    //third loop/test

    int i = 0; 
    int p = 0;

    for(String x : customerName) { 
        for(i = 0; i < customerName.length - 1; ++i) { 
            if((customerName[i] == "Rick")){
                for (p = i; p < customerName.length; ++p){
                customerName[i] = customerName[i +1];
                }
            }
            }

            System.out.println(x);
        }
        System.out.println(" ");
    }


}
公共类CustomerListerArray{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
//创建数组
String[]customerName=新字符串[7];
客户名称[0]=“Beth”;
客户名称[1]=“Jerry”;
客户名称[2]=“Rick”;
客户名称[3]=“夏季”;
客户名称[4]=“Morty”;
//第一回路/测试
对于(字符串x:customerName){
系统输出println(x);
}
System.out.println(“”);
//第二回路/测试
客户名称[5]=客户名称[3];
客户名称[6]=客户名称[4];
客户名称[3]=“Rick”;
客户名称[4]=“Jessica”;
对于(字符串x:customerName){
系统输出println(x);
}
System.out.println(“”);
//第三回路/测试
int i=0;
int p=0;
对于(字符串x:customerName){
对于(i=0;i
在第三个循环测试中,我尝试从数组中取出“Rick”,删除它们,并将其余元素上移。输出应如下所示:

“贝丝 杰瑞 杰西卡 夏天 莫蒂“

现在,程序输出以下内容:

“贝丝 杰瑞 瑞克 夏天 莫蒂 无效的 空的

贝丝 杰瑞 瑞克 瑞克 杰西卡 夏天 莫蒂

贝丝 杰瑞 杰西卡 杰西卡 杰西卡 夏天 莫蒂“

我不明白为什么最后要打印三个“杰西卡”。任何帮助都将不胜感激

首先——做
customerName[i]=“Rick”
是一个典型的新手错误;不管它看起来像什么,它都不会告诉您该值是否为“Rick”(除非您做了一些特殊的事情,我们将不深入讨论)。它将告诉您位于
customerName[i]
的对象是否与您的文字“Rick”是同一个对象,这在生产环境中是不可能的。您需要String类的
equals()
方法;查一查,记住这个。例如,
if(customerName[i].equals(“Rick”)

有关更多信息,请阅读以下内容:

至于Jessica,如果你将该值从
[i]
移动到
[j]
,那么她仍然在
[i]
;如果要“删除”jessica,应将
[i]
的值设置为其他值。

试试这个

private static void output() {

    String[] str_array = {"Beth", "Jerry", "Rick", "Summer", "Morty"};
    List<String> list = new ArrayList<>(Arrays.asList(str_array));
    list.remove("Rick");
    str_array = list.toArray(new String[0]);

    for (String x : str_array) {
        System.out.println(x);
    }
}
private静态无效输出(){
String[]str_数组={“Beth”、“Jerry”、“Rick”、“Summer”、“Morty”};
List List=newarraylist(Arrays.asList(str_array));
列出。删除(“Rick”);
str_array=list.toArray(新字符串[0]);
用于(字符串x:str_数组){
系统输出println(x);
}
}
请记住在类声明上方导入适当的API

import java.util.ArrayList

导入java.util.array


import java.util.List

您尝试过调试吗?@shmosel我尝试过,但对断点感到困惑。我正在使用Eclipse。循环中最里面的语句
customerName[I]=customerName[I+1]在我看来是错的。我想你的意思是在数组索引中使用
p
,而不是
I
。作为记录,
=
可能会在本例中起作用,因为值是硬编码的。为什么[I]仍然是jessica?我想,每重复一次,我都会被重新定义loop@Ryan如果[i]仍然是杰西卡,那么做“customerName[i]=”;客户名称[i]=客户名称[i+1];`。这应该是可行的,代码清空索引[i],然后立即用下一个元素填充它。(注意:我还没有测试过)。@fs2ly我试过,但没用,它和我以前的有什么不同?请解释一下它的不同之处,以及最初的错误。