Java 如何在字符串数组中搜索特定的单词位置

Java 如何在字符串数组中搜索特定的单词位置,java,arrays,return,Java,Arrays,Return,有几个问题。我正在创建一个方法来搜索元素对象数组(其中每个元素对象都已用[atomicNumber缩写名称atomicWeight]初始化)。我还需要返回“对元素的引用”-不确定如何返回。用户在main中输入一个缩写,然后在数组上使用findAbbreviation方法。toString方法将每个数据类型格式化并作为字符串返回。如何在整个数组的任何给定对象中搜索缩写位置。以及如何返回对该“元素”对象的引用 public class PeriodicTable { private fi

有几个问题。我正在创建一个方法来搜索元素对象数组(其中每个元素对象都已用[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;
    }
公共类周期表{
私有最终整数最大元素=200;
私有周期元素[]表;
私有化;
public PeriodicTable()引发IOException{
表=新周期元素[最大元素];
扫描仪输入=新扫描仪(新文件(“此处的文件名”);
int指数=0;
while(input.hasNext()&&index
首先,您需要一个数组或元素集合。这可能是您当前正在编写的类的一个实例变量,其中包括“findAbbreviation”

第二,“元素”可以简单地有一个属性变量,如“缩写”作为元素类的实例变量,您可能只需要调用列表上的findAbbreviation并在缩写实例变量中搜索该缩写。您不太可能搜索实际名称来查找缩写,因为,例如:Gold的“缩写”是AU

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

如果您只是查看元素的缩写列表(如您当前的代码所示),那么您可能只需要修改当前的代码即可正确进行equals比较:

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;
}
第二,您需要更新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;
}
public periodiceelement findAbbreviation(字符串缩写P){
布尔值=false;
int指数=0;
而(!found&&index
您是如何进入此循环的?您的条件开始为false。您也无法通过尝试使用字符串作为索引来从数组中获取元素。您从何处调用此方法?您必须找到带有循环的字符串,并在搜索时跟踪索引。如果您想要abbreviationP的索引然后,您需要使用for循环并搜索数组,直到找到它。此时,您有找到它的索引。我添加了更多代码。如果找不到元素,我将返回“null”。但我正在打印每个元素。是“return null”;(现在是这样)在java中可以接受?是的,返回null对象是可以接受的。由于返回类型是字符串,因此返回空字符串或“”更为常规。我已经更新了我前面答案下面的代码。听起来您实际上想返回PeriodiElement对象而不是字符串。如果您想提供“字符串”要获得有意义的结果,必须重写对象类中的“toString”方法。如何返回实际元素?返回表[index];这是否正确?返回类型是什么?是的,返回“PeriodiceElement”在表中名为“index”的索引处是您希望执行的操作。我更新了代码以反映这一点。
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;
}