使用ONLY If语句查找数组中的元素

我之前发过关于类似主题的post。 但是,我想我会澄清一些事情并改变我的问题。

所以我正在做的这个项目令我感到困惑。 我只有5个星期了,问题是要求我创建一个方法,在一组照片中返回一张照片的标题。 每张照片都有一个标题。 这是代码:

public class Album { private String albumtitle; private ArrayList photos; /** * This constructor should initialize the * instance variables of the class. */ public Album(String title) { this.albumtitle = title; photos = new ArrayList(); } /** When passed a title, this method should * return the first Photo object in the album with * a matching title. If there is no such object, it * should return null. * * @param title A title to search for * @return A Photo object, or null */ public Photo searchByTitle(String title) { //TODO enter code here } } 

现在,我的讲师说不要使用for循环,因为项目是从第1章到第4章(第5章是循环/迭代)

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

这是讲师在不使用for循环的情况下对书籍所做的事情的一个例子。 但是,请注意它有( int index)作为参数然后使用String title = bookTitles.get(index)

我的观点是,如何在不使用for循环的情况下完成它? 我不希望他们感觉到我已经从互联网上复制了一些我们没有学到的东西。

谢谢,

如果您仅限于避免使用for-loop并仅使用if-else ,则递归调用是另一种选择:

 public Photo searchByTitle(String title) { return searchByIndex(title, 0); } private Photo searchByIndex(String title, int index) { if (index < photos.size()) { // Has next? If yes ... Photo next = photos.get(index); // Get next if (!title.equals(next.getPhotoName())) { // Does the title match? If not... return searchByIndex(title, ++index); // Check the next one } else return next; // Otherwise you found it } return null; // ... if no next, return null } 

我假设Photo类有一个字段String photoName可以使用getter进行访问,该getter将与String title进行比较。

  1. Photo上实现Comparable ,如果标题相同则返回true。
  2. 构造具有给定类型的临时Photo对象。
  3. 利用ArrayList上的indexOf方法查找具有Photo标题的Album索引。
  4. 使用get(int)获取Album

我已经从上面的评论中实现了代码。
这里的想法是在searchByTitle方法中构建一个临时对象,并将其传递给List.indexOf方法,该方法具有覆盖Object.equalsPhoto类。

 public class Album { class Photo { private String title; public Photo(String title) { this.title = title; } public String getTitle() { return title; } @Override public boolean equals(Object anObject) { return title.equals(((Photo)anObject).getTitle()); } } private String albumtitle; private ArrayList photos; /** * This constructor should initialize the * instance variables of the class. */ public Album(String title) { this.albumtitle = title; photos = new ArrayList<>(); } /** When passed a title, this method should * return the first Photo object in the album with * a matching title. If there is no such object, it * should return null. * * @param title A title to search for * @return A Photo object, or null */ public Photo searchByTitle(String title) { Photo tmp = new Photo(title); int index = photos.indexOf(tmp); if (index >= 0) return photos.get(index); return null; } } 

这是equals一个非常基本的实现,它没有考虑null参数及其类型。

您可以带另一个StringList,它保留照片标题的名称。 然后在搜索function搜索中使用String arrayList中的照片标题。 如果找到索引,则从photoTitles返回该索引的Photo对象,因为您以相同的顺序插入两个arrayList。

 public class Album { private String albumtitle; private ArrayList photos; private ArrayList photoTitles; public Album(String title) { this.albumtitle = title; photos = new ArrayList<>(); photoTitles = new ArrayList<>(); } public Photo searchByTitle(String title) { int index = photoTitles.indexOf(title); if(index >= 0) { return photos.get(index); } return null; } } 

仅仅因为我喜欢递归的想法,但是在List上使用index搜索并不是最优的,让我们使用迭代器。 递归方法将简单地检查是否有一个值要采取,检查并再次调用,直到找到值或到达结尾。

 //Just hide the used of an iterator public static Photo getFirstWithTitle(List list, String value){ return getFirstWithTitle(list.iterator(), value); } //recursive method private static Photo getFirstWithTitle(Iterator it, String value){ if(it.hasNext()){ Photo p = it.next(); return p.getTitle().equals(value)? p : getFirstWithTitle(it, value); } else return null; } 

您可以使用内置的binarySearch和比较器。 可能是最优雅的方式以及我通常如何做到这一点

 public Photo searchByTitle(String title) { Photo item = null Comparator comparator = new Comparator() { public int compare(Photo it1, Photo it2) { return it1.getTitle().compareTo(it2.getTitle()); } }; Collections.sort(photos, comparator); //This should go after you set the items, ie. you sort it once int index = Collections.binarySearch(photos, comparator); if (index >= 0) { item = photos.get(index); } return item } 

使用Java,您甚至不需要’if’语句。 这可以通过以下方式实现:

 public Photo searchByTitle(String title) { return photos.stream().filter(photo -> title. equals(photo.getTitle())).findAny().get(); } 

ps:我无法访问您的发音问题(提供的链接)