【问题标题】:How to search specific word position in array of Strings如何在字符串数组中搜索特定单词的位置
【发布时间】:2017-03-16 03:00:32
【问题描述】:

几个问题。我正在创建一个搜索元素对象数组的方法(其中每个元素对象都已使用 [atomicNumber 缩写名称 atomicWeight] 进行了初始化)。我还需要返回“对元素的引用”——不完全确定如何执行此操作。用户在 main 中输入一个缩写,然后在数组上使用 findAbbreviation 方法。 toString 方法格式化并将每个数据类型作为字符串返回。我如何在整个数组的任何给定对象中搜索缩写位置。以及如何返回对该“元素”对象的引用。

 public class PeriodicTable {

    private final int MAX_ELEMENTS = 200;
    private PeriodicElement[] table;
    private int actualSize;

    public PeriodicTable() throws IOException{
        table = new PeriodicElement[MAX_ELEMENTS];

        Scanner input = new Scanner(new File("file name here"));
        int index = 0;
        while(input.hasNext() && index < MAX_ELEMENTS) {
            int aN = input.nextInt();
            String abbr = input.next();
            String name = input.next();
            double aW = input.nextDouble();
            table[index] = new PeriodicElement(aN, abbr, name, aW);
            index++;

        }
        input.close();
        actualSize = index;
    }

    public String findAbbreviation(String abbreviationP){
        boolean found = false;
        int index = 0;
        while(found && index < MAX_ELEMENTS){
            if (table[index] = table[abbreviationP]){
                found = true;

                return table[index].toString;
            }              
            index++;    
        }
        return null;
    }
}

class PeriodicElement {
    private int atomicNumber;
    private String abbreviation, name;
    private double atomicWeight;

    public PeriodicElement(int atomicNumberP,
            String abbreviationP, String nameP,
            double atomicWeightP){
        atomicNumber = atomicNumberP;
        abbreviation = abbreviationP;
        name = nameP;
        atomicWeight = atomicWeightP;
    }

【问题讨论】:

  • 你是怎么进入这个循环的?你的情况一开始是错误的。
  • 您也无法通过尝试使用字符串作为索引从数组中获取元素。你从哪里调用这个方法?您将不得不使用循环查找字符串并在搜索时跟踪索引。如果您想要 abbreviationP 的索引,那么您需要使用 for 循环并搜索数组,直到找到它。在那一刻,您拥有找到它的索引。

标签: java arrays return


【解决方案1】:

首先,您需要一个数组或元素集合。这可能是您当前正在编写的类的实例变量,其中包括“findAbbreviation”。

其次,“元素”可以简单地将“缩写”之类的属性变量作为元素类的实例变量,并且您可以只调用列表中的 findAbbreviation 并在缩写中专门搜索该缩写实例变量。您不太可能通过搜索实际名称来找到缩写,因为例如:Gold 的“缩写”是 AU。

您能否说明如何定义元素列表以及定义元素的类?

如果您只是查看元素的缩写列表(正如您当前的代码所建议的那样),您可能只需要修复您当前的代码以正确进行相等比较:

public String findAbbreviation(String abbreviationP){
        boolean found = false;
        int index = 0;
        while(!found && index < MAX_ELEMENTS){  //this should be !found instead of found
            if (table[index].equals(abbreviationP)) { // you previously had an assignment statement in this if
                found = true;

                return table[index].toString;
            }              
            index++;    
        }
    return null;
}

更新答案以反映问题的更新:

首先,您需要在 PeriodicElement 类中提供一个方法来获取实例变量“abbreviation”。

这是一个标准的“getter”:

public String getAbbreviation() {
    return abbreviation;
}

其次,您需要更新您的 findAbbreviation 方法以利用这个新的 getter:

public PeriodicElement findAbbreviation(String abbreviationP){
        boolean found = false;
        int index = 0;
        while(!found && index < MAX_ELEMENTS){  //this should be !found instead of found
            if (table[index].getAbbreviation().equals(abbreviationP)) { // you previously had an assignment statement in this if
                found = true;

                return table[index];  //This will return the object at the index where it was found. 
                                      // Notice I also changed the return type on your function.
            }              
            index++;    
        }
    return null;
}

【讨论】:

  • 我添加了更多代码。如果找不到该元素,我将返回“null”。但我正在打印出每个元素。是'返回空'; (就像现在一样)在 java 中可以接受吗?
  • 是的,返回一个空对象是可以接受的。由于您的返回类型是字符串,因此返回空字符串或“”更为常规。我已经更新了我之前答案下方的代码。
  • 听起来您实际上想要返回 PeriodicElement 对象而不是字符串。如果您想提供元素的“String”版本,为了获得有意义的结果,您必须覆盖 Object 类中的“toString”方法。
  • 如何返回实际元素?返回表[索引];这是正确的吗?返回类型是什么?
  • 是的,在您的表中返回名为“index”的索引处的“PeriodicElement”是您想要做的。我更新了代码以反映这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多