Java 使用传统for循环删除字符串数组中的某些元素
作业描述: 编写一个传统的for循环,检查每个元素是否有字符串“Meg”,如果在数组中找到,则将其删除,移动其余元素,并显示名称数组 我无法理解这个概念。我知道如何初始化数组并在循环外重新分配元素,但不知道如何在for循环内替换它们。这就是我所拥有的。这是我的第一节编程课,我现在很挣扎。如果有经验丰富的人能详细解释去除“梅格”的过程,我将不胜感激 节目:Java 使用传统for循环删除字符串数组中的某些元素,java,arrays,Java,Arrays,作业描述: 编写一个传统的for循环,检查每个元素是否有字符串“Meg”,如果在数组中找到,则将其删除,移动其余元素,并显示名称数组 我无法理解这个概念。我知道如何初始化数组并在循环外重新分配元素,但不知道如何在for循环内替换它们。这就是我所拥有的。这是我的第一节编程课,我现在很挣扎。如果有经验丰富的人能详细解释去除“梅格”的过程,我将不胜感激 节目: public class CustomerListerArray{ public static void main(String[] a
public class CustomerListerArray{
public static void main(String[] args){
String[] customerNames = new String[7];
customerNames[0] = "Chris";
customerNames[1] = "Lois";
customerNames[2] = "Meg";
customerNames[3] = "Peter";
customerNames[4] = "Stewie";
for(String element : customerNames){
System.out.println(element);
}
System.out.println();
customerNames[6] = customerNames[4];
customerNames[5] = customerNames[3];
customerNames[4] = "Brian";
customerNames[3] = "Meg";
for(String element : customerNames){
System.out.println(element);
}
System.out.println();
//******************************************************************
for(int i = 0; i < customerNames.length; i++) {
if(customerNames.equals("Meg")){
customerNames[i] = null;
for(int j = i; customerNames.equals(null); j++){
customerNames[i] = customerNames[j + 1] ;
System.out.println(customerNames[i]);
}
}
}//******************************************************************
}
}
公共类CustomerListerArray{
公共静态void main(字符串[]args){
String[]customerNames=新字符串[7];
客户名称[0]=“克里斯”;
客户名称[1]=“意向书”;
客户名称[2]=“Meg”;
客户名称[3]=“彼得”;
客户名称[4]=“Stewie”;
for(字符串元素:customerNames){
系统输出打印项次(元素);
}
System.out.println();
客户名称[6]=客户名称[4];
客户名称[5]=客户名称[3];
客户名称[4]=“Brian”;
客户名称[3]=“Meg”;
for(字符串元素:customerNames){
系统输出打印项次(元素);
}
System.out.println();
//******************************************************************
对于(int i=0;i
解决此问题的最简单方法是声明另一个大小为customerNames
的数组,迭代customerNames
,如果customerName
为“Meg”,则不执行任何操作,否则将其添加到新数组中。最后,您将拥有一个数组,其中包含除“meg”之外的所有名称。在这个for循环中,您可以计算非“meg”名称
另一种低效的方法是,每次你找到“meg”,你都会在一个元素上移动所有元素。因此,如果在i=2
处找到“meg”,则从2开始遍历数组,customerNames[i]=customerNames[i+1]
这将逐个移动所有元素。此方法将起作用,因为您正在用后面的元素替换存储“meg”的索引
把它想象成一系列的盒子,当你把一个盒子从中间拿出来时,你想把所有其他的盒子都向上移动,以消除空的空间。或者,最好在excel数字1-10中设置一列,如果您清空一个随机单元格,则必须逐个上移以填补空白。(很明显,excel中有一个变通方法,只是一个示例)
我会给你解决方法,但在我看来,作为一个经历这种痛苦的初学者,自己去弄清楚是很重要的 解决此问题的最简单方法是声明另一个数组,大小为
customerNames
,在customerNames
中迭代,如果customerName
为“Meg”,则不执行任何操作,否则将其添加到新数组中。最后,您将拥有一个数组,其中包含除“meg”之外的所有名称。在这个for循环中,您可以计算非“meg”名称
另一种低效的方法是,每次你找到“meg”,你都会在一个元素上移动所有元素。因此,如果在i=2
处找到“meg”,则从2开始遍历数组,customerNames[i]=customerNames[i+1]
这将逐个移动所有元素。此方法将起作用,因为您正在用后面的元素替换存储“meg”的索引
把它想象成一系列的盒子,当你把一个盒子从中间拿出来时,你想把所有其他的盒子都向上移动,以消除空的空间。或者,最好在excel数字1-10中设置一列,如果您清空一个随机单元格,则必须逐个上移以填补空白。(很明显,excel中有一个变通方法,只是一个示例)
我会给你解决方法,但在我看来,作为一个经历这种痛苦的初学者,自己去弄清楚是很重要的 要解决这个问题,需要了解一些不同的事情
for (int i = 0; i < array.length; i++) {
String currentString = array[i];
}
很简单。我们遍历了数组,找到了我们想要的值,并在找到所需内容时向控制台打印了一条小消息。我们已经走到一半了
删除值
因此,我们确定了解决这个问题还需要采取的两个步骤。但事实证明,我们可以同时执行步骤2和步骤3!让我们想想为什么这是可能的。步骤2要求我们删除一个值。在Java数组中,我们通过用其他内容覆盖值来删除它们。由于第3步告诉我们将值向左移动,我们实际上最终还是覆盖了第2步的值
如果要删除Y
,请考虑此数组:
|X|Y|Z|
|X|_|Z|
我们可以先“删除”Y
:
|X|Y|Z|
|X|_|Z|
然后向左移动Z
:
|X|Z|_|
或者我们可以在一个步骤中将Z
向左移动:
|X|Y|Z| => |X|Z|_|
我们将尝试第二种方法,因为它看起来更简单。让我们找到我们正在寻找的字符串,然后,对于它右边的每个元素,开始将元素向左移动一个位置
int index = 0;
String searchStr = "FindMe";
for (int i = 0; i < array.length; i++) {
if (searchStr.equals(array[i])) {
// Make a note of where we found it, then stop searching
index = i;
break;
}
}
for (int i = index; i < array.length - 1; i++) {
// Replace each value with the next value
array[i] = array[i+1];
}
只需用null
覆盖最后一个值,这就是我们在Java中表示“无”概念的方式。我们已经将最后一个值向左移动了
int index = -1;
String searchStr = "FindMe";
for (int i = 0; i < array.length; i++) {
if (searchStr.equals(array[i])) {
index = i;
break;
}
}
if (index > -1) {
for (int i = index; i < array.length - 1; i++) {
array[i] = array[i+1];
}
}
array[array.length - 1] = null;