Java回归困境
好的,我有下面的代码,不管它返回给我什么-1。我想让它这样,如果id匹配,那么它返回和索引,但是如果在运行整个数据集之后它不匹配,那么它返回一个负的。我在这里哪里出了问题:Java回归困境,java,arrays,search,Java,Arrays,Search,好的,我有下面的代码,不管它返回给我什么-1。我想让它这样,如果id匹配,那么它返回和索引,但是如果在运行整个数据集之后它不匹配,那么它返回一个负的。我在这里哪里出了问题: public class StudentCollection { private String[] ids = new String[] {"Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Emp
public class StudentCollection {
private String[] ids = new String[] {"Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty"}; // keeps identification numbers of students
private String [] names = new String[] {"Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty","Empty"};; // keeps the names of students
private int size = 0; // number of students currently in the collection
private int findIndex(String id) {
int noIndex = 1;
for (int i=0;i<ids.length;i++){
if((ids[i].equalsIgnoreCase(id))){
System.out.println("The index of this student is " +i);
}
else {
noIndex = -1;
System.out.println(noIndex);
break;}
}
return noIndex;
}
公共班级学生集合{
私有字符串[]id=新字符串[]{“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”};//保留学生的身份证号
私有字符串[]名称=新字符串[]{“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”、“Empty”};//保留学生姓名
private int size=0;//集合中当前的学生数
私有int findIndex(字符串id){
int noIndex=1;
对于(int i=0;i我想你需要这样的东西:
private int findIndex(String id) {
for (int i=0; i<ids.length; i++){
if(ids[i].equalsIgnoreCase(id)){
System.out.println("The index of this student is " +i);
return i;
}
}
return -1;
}
private int findIndex(字符串id){
对于(int i=0;i我想你需要这样的东西:
private int findIndex(String id) {
for (int i=0; i<ids.length; i++){
if(ids[i].equalsIgnoreCase(id)){
System.out.println("The index of this student is " +i);
return i;
}
}
return -1;
}
private int findIndex(字符串id){
对于(int i=0;i这里是一个解决方案,如果找到索引,则返回其编号;否则,如果在检查整个数组后没有找到,则返回-1并打印相应的字符串
private int findIndex(String id) {
int noIndex = -1;
for (int i = 0; i < ids.length; i++) {
if (ids[i].equalsIgnoreCase(id)) {
System.out.println("The index of this student is " + i);
return i;
}
}
System.out.println(noIndex);
return noIndex;
}
这里的解决方案是,如果找到索引,则返回其编号;否则,如果在检查整个数组后没有找到,则返回-1并打印相应的字符串
private int findIndex(String id) {
int noIndex = -1;
for (int i = 0; i < ids.length; i++) {
if (ids[i].equalsIgnoreCase(id)) {
System.out.println("The index of this student is " + i);
return i;
}
}
System.out.println(noIndex);
return noIndex;
}
现在,当ids[i].equalsIgnoreCase(id)
为true时,它将noIndex
设置为-1(在else语句中),并中断for循环,使其返回-1。如果为false,它将打印出索引。
就像其他人已经发布的一样,下面是查找索引的代码
private int findIndex(String id) {
for (int i=0;i<ids.length;i++){
if(ids[i].equalsIgnoreCase(id)){
return i;
}
}
return -1;
}
private int findIndex(字符串id){
for(int i=0;i现在就是这样,当ids[i].equalsIgnoreCase(id)
为true时,它将noIndex
设置为-1(在else语句中),并中断for循环,使其返回-1。如果为false,它将打印出索引。
就像其他人已经发布的一样,下面是查找索引的代码
private int findIndex(String id) {
for (int i=0;i<ids.length;i++){
if(ids[i].equalsIgnoreCase(id)){
return i;
}
}
return -1;
}
private int findIndex(字符串id){
对于(int i=0;i代码中的ids
是什么?提示:您在哪里将noIndex设置为要返回的值?何时中断?为什么要否定equalsIgnoreCase的结果?代码中的ids
是什么?提示:您在哪里将noIndex设置为要返回的值?何时中断?为什么要否定equalsIgno的结果reCase?第二种方法区分大小写,因此不起作用。然后更正它,否则会有一些向下投票者出现(不是我;)。第二种方法区分大小写,因此不起作用。然后更正它,否则会有一些向下投票者出现(不是我;)。OP在返回前也会打印。OP在返回前也会打印。谢谢你,这很有效!我理解它为什么会这样做!我也理解它为什么会这样做