gpt4 book ai didi

java - 当“类别”等于 null 时跳过第一个循环周期

转载 作者:行者123 更新时间:2023-11-30 05:41:21 25 4
gpt4 key购买 nike

我正在从 json 文件读取数据,并且我想按类别获取项目

 public List<Item> getItemsByCategory(String category) {
List<Item> matchingCategory = new ArrayList<>();
for (Item item : books.getItems()) {
List<String> categories1 = item.getVolumeInfo().getCategories();
// categories1 == null for the first item so the below for loop fails
for (String cat : categories1) {
if (cat.equals(category)) {
matchingCategory.add(item);
} else throw new MyResourceNotFoundException(String.format("category with type %s is not found"));
}
}
return matchingCategory;
}

我的 json 文件中的第一个项目没有类别,这就是为什么类别在第一个循环中为空,我如何跳过它并继续在 json 文件中的项目中,而不是获得空指针异常

我尝试使用break,但它结束了循环而不开始下一个

这是我从这里读取数据的 json。

{
"requestedUrl": "https://www.googleapis.com/books/v1/volumes?q=java&maxResults=40",
"items": [
{
"kind": "books#volume",
"id": "7tkN1CYzn2cC",
"etag": "pfjjxSpetIM",
"selfLink": "https://www.googleapis.com/books/v1/volumes/7tkN1CYzn2cC",
"volumeInfo": {
"title": "A Hypervista of the Java Landscape",
"publisher": "InfoStrategist.com",
"industryIdentifiers": [
{
"type": "ISBN_13",
"identifier": "9781592432172"
},
{
"type": "ISBN_10",
"identifier": "1592432174"
}
],
"readingModes": {
"text": true,
"image": true
},
"printType": "BOOK",
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "1.0.1.0.preview.3",
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=7tkN1CYzn2cC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=7tkN1CYzn2cC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.pl/books?id=7tkN1CYzn2cC&pg=PP1&dq=java&hl=&cd=1&source=gbs_api",
"infoLink": "http://books.google.pl/books?id=7tkN1CYzn2cC&dq=java&hl=&source=gbs_api",
"canonicalVolumeLink": "https://books.google.com/books/about/A_Hypervista_of_the_Java_Landscape.html?hl=&id=7tkN1CYzn2cC"
},
"saleInfo": {
"country": "PL",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "PL",
"viewability": "PARTIAL",
"embeddable": true,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": true,
"acsTokenLink": "http://books.google.pl/books/download/A_Hypervista_of_the_Java_Landscape-sample-epub.acsm?id=7tkN1CYzn2cC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
},
"pdf": {
"isAvailable": true,
"acsTokenLink": "http://books.google.pl/books/download/A_Hypervista_of_the_Java_Landscape-sample-pdf.acsm?id=7tkN1CYzn2cC&format=pdf&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
},
"webReaderLink": "http://play.google.com/books/reader?id=7tkN1CYzn2cC&hl=&printsec=frontcover&source=gbs_api",
"accessViewStatus": "SAMPLE",
"quoteSharingAllowed": false
}
},
{
"kind": "books#volume",
"id": "-SYM4PW-YAgC",
"etag": "dXytGSDckJk",
"selfLink": "https://www.googleapis.com/books/v1/volumes/-SYM4PW-YAgC",
"volumeInfo": {
"title": "The Religion of Java",
"authors": [
"Clifford Geertz"
],
"publisher": "University of Chicago Press",
"publishedDate": "1976-02-15",
"description": "Written with a rare combination of analysis and speculation, this comprehensive study of Javanese religion is one of the few books on the religion of a non-Western people which emphasizes variation and conflict in belief as well as similarity and harmony. The reader becomes aware of the intricacy and depth of Javanese spiritual life and the problems of political and social integration reflected in the religion. The Religion of Java will interest specialists in Southeast Asia, anthropologists and sociologists concerned with the social analysis of religious belief and ideology, students of comparative religion, and civil servants dealing with governmental policy toward Indonesia and Southeast Asia.",
"industryIdentifiers": [
{
"type": "ISBN_10",
"identifier": "0226285103"
},
{
"type": "ISBN_13",
"identifier": "9780226285108"
}
],
"readingModes": {
"text": true,
"image": true
},
"pageCount": 392,
"printType": "BOOK",
"categories": [
"Religion"
],
"averageRating": 4.0,
"ratingsCount": 4,
"maturityRating": "NOT_MATURE",
"allowAnonLogging": false,
"contentVersion": "2.1.2.0.preview.3",
"imageLinks": {
"smallThumbnail": "http://books.google.com/books/content?id=-SYM4PW-YAgC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
"thumbnail": "http://books.google.com/books/content?id=-SYM4PW-YAgC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
},
"language": "en",
"previewLink": "http://books.google.pl/books?id=-SYM4PW-YAgC&printsec=frontcover&dq=java&hl=&cd=2&source=gbs_api",
"infoLink": "http://books.google.pl/books?id=-SYM4PW-YAgC&dq=java&hl=&source=gbs_api",
"canonicalVolumeLink": "https://books.google.com/books/about/The_Religion_of_Java.html?hl=&id=-SYM4PW-YAgC"
},
"saleInfo": {
"country": "PL",
"saleability": "NOT_FOR_SALE",
"isEbook": false
},
"accessInfo": {
"country": "PL",
"viewability": "PARTIAL",
"embeddable": true,
"publicDomain": false,
"textToSpeechPermission": "ALLOWED",
"epub": {
"isAvailable": true,
"acsTokenLink": "http://books.google.pl/books/download/The_Religion_of_Java-sample-epub.acsm?id=-SYM4PW-YAgC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
},
"pdf": {
"isAvailable": false
},
"webReaderLink": "http://play.google.com/books/reader?id=-SYM4PW-YAgC&hl=&printsec=frontcover&source=gbs_api",
"accessViewStatus": "SAMPLE",
"quoteSharingAllowed": false
},
"searchInfo": {
"textSnippet": "Written with a rare combination of analysis and speculation, this comprehensive study of Javanese religion is one of the few books on the religion of a non-Western people which emphasizes variation and conflict in belief as well as ..."
}
} etc

如上所述,第一个项目没有类别,但第二个项目有,所以我应该能够获得第二个项目

最佳答案

这是我所做的,也使用 continue 和 break,但我必须删除我的异常处理程序,

  public List<Item> getItemsByCategory(String category) {
List<Item> matchingCategory = new ArrayList<>();
for (Item item : books.getItems()) {
List<String> categories1 = item.getVolumeInfo().getCategories();
if(categories1 == null) continue;
for (String cat : categories1) {
if (cat.equals(category)) {
matchingCategory.add(item);
} else break; //throw new MyResourceNotFoundException(String.format("category with type %s is not found", category));
}
}
return matchingCategory;
}

如何放回抛出新异常

关于java - 当“类别”等于 null 时跳过第一个循环周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com