在字符串数组中查找字符串值的搜索方法

时间:2018-08-31 08:51:21

标签: java

因此,我正在尝试创建一种在照片的ArrayList中查找标题的方法。

public class Album {

    private String albumtitle;
    private ArrayList<Photo> photos;

    /**
     * This constructor should initialize the
     * instance variables of the class.
     */
    public Album(String title) {

        this.albumtitle = title;
        photos = new ArrayList<>();

    }

这是我尝试搜索照片特定标题所必须的代码。我不确定是否应该将(int index)(String title)放在方法参数中。

public Photo searchByTitle(int index) {

        if (index >= 0 && index < photos.size()) {
            String title = photos.get(index);
            System.out.println(title);
        }
        return null;
    }

我是初学者程序员,我觉得一些指导会有所帮助。

编辑:很多人告诉我要使用for循环。我的项目要求我不要对方法进行循环,因此为什么要以这种方式显示它。

我给你讲一个讲师给我们的例子:

https://lms.uwa.edu.au/bbcswebdav/pid-1134902-dt-content-rid-16529804_1/courses/CITS1001_SEM-2_2018/lectures/BooksReadJournal.java.pdf

她不使用循环。

5 个答案:

答案 0 :(得分:4)

您可以使用流API:

Arrays.stream(photos).filter(p -> p.getTitle().equalsIgnoreCase(searchTitle)).findFirst().orElseGet(...);

您遍历此数组中每张称为p的照片,并将p的标题与您搜索的标题进行比较,并返回第一个匹配项。

或仅使用普通的for循环:

for(int i = 0; i < photos.size(); i++) {
  if(photos[i].getTitle().equalsIgnoreCase(searchTitle)){ return photos[i]; }
  return new ErrorPhoto(); //or some error state
}

更多的for-each-loop:

for(Photo p: photos) {
  if(p.getTitle().equalsIgnoreCase(searchTitle)) { return p; }
  return new ErrorPhoto(); //or some error state
}

答案 1 :(得分:3)

您遍历每张照片,并将其标题与搜索的标题进行比较,并返回第一个匹配项:

for(int i = 0; i < photos.size, i++) {
    if(photos.get(i).getTile().equals("title name"){
         System.out.println("Found the title");
    }
}

答案 2 :(得分:2)

您当前的代码将始终返回null。如果这是用于返回找到的对象的搜索方法,则如果找到该对象,则返回该对象。如果找不到,则返回null:

public Photo searchByTitle(String title) {    
    for(Photo p : photos)
        if(p.getTitle().equals(title))
            return p;
    return null;
}
  

这是我尝试搜索照片特定标题所必须的代码。我不确定是否应该将(int index)或(String title)放在方法参数中

由于方法名称本身建议使用searchByTitle,因此使用此方法的任何人都希望它收到String title

如果要通过索引进行搜索,那么可以有另一种方法:

public Photo searchByIndex(int index){

}

答案 3 :(得分:1)

您有两个搜索选项,一个Photo通过其标题或索引向上查找。如果您的类同时具有这两种方法,则将如下所示:

public class Album {

    private String albumtitle;
    private ArrayList<Photo> photos;

    /**
     * This constructor should initialize the instance variables of the class.
     */
    public Album(String title) {
        this.albumtitle = title;
        photos = new ArrayList<>();
    }

    /**
     * Searches the {@link Photo} with the given title.
     * @param title the title of the desired {@link Photo}
     * @return the {@link Photo} with the given title or 
     *  <code>null</code> if it is not in the list
     */
    public Photo searchByTitle(String title) {
        // initialize with null to return that if the photo was not found
        Photo photo = null;

        // iterate all photos and check if one of them has the title
        for (int i = 0; i < photos.size(); i++) {
            if (title.equals(photos.get(i).getTitle())) {
                photo = photos.get(i);
                // exit the loop when the photo was found
                break;
            }
        }

        return photo;
    }

    /**
     * Searches the {@link Photo} with the given index.<br>
     * Checks if the index is valid in the list of {@link Photo}s
     * @param index the index of the {@link Photo}
     * @return the {@link Photo} with the given index or <code>null</code> 
     *  if it is not in the list or the index is invalid.
     */
    public Photo searchByIndex(int index) {
        try {
            // use the method of the list to get the photo
            return photos.get(index);
        } catch (IndexOutOfBoundsException ioe) {
            // print some error message for the case of an invalid index
            System.err.println("The given index is not available!");
            return null;
        }
    }
}

答案 4 :(得分:0)

package simple;
import java.util.ArrayList;
public class Album {
private static String albumtitle;
public Album(String title) {
    Album.albumtitle = title;
}

public Photo searchByTitle(ArrayList<Photo> photos) {
    if (index >= 0 && index < photos.size()) {
        Photo title = photos.get(index);
        if (title.getTitle().equals(albumtitle))
            System.out.println("tile Found" + title);
    }
    return null;
}

public static void main(String[] args) {
    ArrayList<Photo> photos = new ArrayList<>();
    //Searching title2 or u can pass as parameter
    Album album = new Album("title2");
    for (int i = 0; i < 5; i++) {
        Photo photo = new Photo();
        photo.setTitle("Title" + i);
        photos.add(photo);

    }
    album.searchByTitle(photos);

}

}

package simple;
public class Photo {
String title;
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

}

正在寻找这个吗??