简体   繁体   English

检查数组中是否存在字符串

[英]Checking if a string exists in array

I'm trying to check if a name has already been used in the array, but it's only working for the spot [0].我正在尝试检查名称是否已在数组中使用,但它仅适用于 [0] 位置。 I'm assuming its from the for loop in boolean only going through once and not incrementing up to check the other spots?我假设它来自布尔值的 for 循环只经过一次而不是递增来检查其他点?

Tried changing different if's and while loops尝试改变不同的 if 和 while 循环

if (depotCount < 4){ // Runs if the depots are less than 4
    System.out.println("Enter your depots name");
    name = console.next().toLowerCase();
    if (check(depots, name) == true){
       System.out.println("Depot name already exists");
       break;
    }
    else {
      addDepot(depots, name);   
    }              
} else { 
     System.out.println("Only 4 depots are allowed");
     break;
}
public boolean check(Depot [] depots, String name){
  boolean check = false;
  for (int i = 0; i < depots.length; i++){
     if(depots[i].getDepotName().equals(name))
       return true;
     else {
       return false;
     }
   }
  return check;

}

So it is working if a first name as "Warehouse" and I try to put "Warehouse" a second time.因此,如果第一个名称为“仓库”并且我尝试第二次输入“仓库”,它会起作用。 but if I try to put the same name as the 2nd slots it doesn't come back as true.但是如果我尝试使用与第二个插槽相同的名称,它就不会恢复原状。

You need remove return false;您需要删除return false; in for loop, if you put there, for loop run only one time with index 0.在 for 循环中,如果你把它放在那里,for 循环只运行一次,索引为 0。

public boolean check(Depot[] depots, String name) {
    boolean check = false;
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
            return true;
    }
    return check;
}

You can only short like this without check variable.你只能像这样在没有检查变量的情况下做空。

public boolean check(Depot[] depots, String name) {
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
            return true;
    }
    return false;
}

Inside your for loop you have an "else return false".在你的 for 循环中,你有一个“else return false”。 what this does is, if you find something that is not equal you immidiatly return false.这是做什么的,如果你发现一些不相等的东西,你会立即返回 false。 However, if you return on any occasion in your method, the method is over thus it doesnt loop through all depots但是,如果您在方法中的任何场合返回,则该方法已结束,因此它不会遍历所有仓库

public boolean check(Depot [] depots, String name){
    for (int i = 0; i < depots.length; i++){
        if(depots[i].getDepotName().equals(name))
            return true;
    }
    return false;
}

The problem is that you always return on the first iteration of the loop.问题是你总是在循环的第一次迭代时返回。

Try modifying your code as follows:尝试修改您的代码如下:

public boolean check(Depot[] depots, String name) {
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
             return true;
    }
    return false;
}

Also, you do not need to compare for true in an if statement.此外,您不需要在 if 语句中比较 true。 That is you can change this:也就是说你可以改变这个:

if (check(depots, name) == true) {

to this:对此:

if (check(depots, name)) {

Also, you might want to check out java's HashMap .此外,您可能想查看 java 的HashMap These have methods such as:这些有方法,例如:

  • containsKey(key) checks to see if the key (depotName) is present or not containsKey(key) 检查密钥 (depotName) 是否存在
  • get (key) retrieves the record by Key get(key) 通过Key检索记录
  • put (key, value) allows you to add (put) values into the map. put (key, value) 允许您将(放置)值添加到地图中。
  • there is no limit (eg you don't need to pre-declare the size like you do with an Array).没有限制(例如,您不需要像使用数组那样预先声明大小)。
  • They are quite fast - especially as the number of entries get's larger.它们非常快 - 特别是当条目数量变大时。

As for the value, that can be anything you like.至于价值,那可以是你喜欢的任何东西。 For example it might be a String containing the companies address.例如,它可能是一个包含公司地址的字符串。 It could be an integer containing the number of employees at that company.它可以是包含该公司员工人数的整数。 Or, and this is the best one, it could be an instance of a class containing every conceivable detail about the company!或者,这是最好的一个,它可以是一个类的实例,其中包含有关公司的每一个可以想象的细节!

Alternatively if you don't need to store a value, you can always use a KeySet, but HashMaps are probably more useful for you.或者,如果您不需要存储值,您始终可以使用 KeySet,但 HashMaps 可能对您更有用。

It's because the you have return false;这是因为你return false; . . This simply ends the method execution (and by doing so the for loop) because the value false have been returned to the method call.这只是结束了方法执行(并通过这样做结束了 for 循环),因为值 false 已返回给方法调用。

public boolean check(Depot[] depots, String name) {
    boolean check = false;
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
            return true;
    }
    return check;
}

In my opinion you should remove: else { return false;在我看来你应该删除: else { return false; } from your code:从你的代码:

public boolean check(Depot[] depots, String name) {
    boolean check = false;
    for (int i = 0; i < depots.length; i++) {
        if (depots[i].getDepotName().equals(name))
            return true;
    }
    return check;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM