无法按阵列列表中的标题显示所有DVD对象

时间:2017-11-02 04:37:02

标签: java arraylist methods

所以我需要制作一个Display方法,按类别显示数组列表中的所有DVD对象。

这正是该方法应该做的事情:

displayDVDsInCategory - 此方法应该有一个类别作为参数。它 应该返回一个arrayList对象,其中包含指定类别中的所有DVD。如果给定类别中没有DVD,则arrayList的大小将为零。

按类别显示DVD - 用户应该能够显示特定的所有DVD 类别。要求用户输入DVD类别。如果集合中没有DVD 匹配请求的类别,向用户显示一条消息,指出没有 所请求类别的DVD。否则,显示DVD标题列表 - 每个标题一个 line - 用于指定类别中的DVD。仅显示标题,而不是全部 信息。

这是我目前无法正常工作的方法,我做错了什么?

public DVD displayDVDsInCategory(String category) 
{
    for (int i=0;i<arraylist.size();i++)
    {
        if(category.equalsIgnoreCase(arraylist.get(i).getCategory())){
            return arraylist.get(i);
        }
    }
    return null;
}

这是我在主方法类

中调用它的方式
else if(selection==4){
   String ser;
   System.out.println("Please enter a DVD category to search for:");
   kbd.nextLine();
   ser=kbd.nextLine();
   System.out.println(x.displayDVDsInCategory(ser));
}

3 个答案:

答案 0 :(得分:1)

到目前为止,您只需从功能中返回一个 for (i = 1; i < RoutePointslength; ++i) { var CurrentLatLong = []; CurrentLatLong.push([data._DriverLocation[i].Latitude, data._DriverLocation[i].Longitude]); localStorage["EndLatLong"] = null; localStorage["EndLatLong"] = JSON.stringify(CurrentLatLong[0]).replace('[', '').replace(']', ''); setTimeout(function() { startRoute(); }, 300); } function startRoute() { polyline = null; poly2 = null; polyline = new google.maps.Polyline({ path: [], strokeColor: 'green', strokeWeight: 3 }); poly2 = new google.maps.Polyline({ path: [], strokeColor: 'green', strokeWeight: 3 }); // Create a renderer for directions and bind it to the map. var rendererOptions = { map: map, strokeColor: "#8b0013" }; directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions); //var start = document.getElementById("start").value; var start_ = localStorage["StartLatLong"]; //initial lat,long var end_ = localStorage["EndLatLong"]; var travelMode = google.maps.DirectionsTravelMode.DRIVING; localStorage["StartLatLong"] = null; localStorage["StartLatLong"] = end_; var request = { origin: start_, destination: end_, travelMode: travelMode }; // Route the directions and pass the response to a // function to create markers for each step. directionsService.route(request, function (response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); var bounds = new google.maps.LatLngBounds(); var route = response.routes[0]; startLocation = new Object(); endLocation = new Object(); // For each route, display summary information. var path = response.routes[0].overview_path; var legs = response.routes[0].legs; for (i = 0; i < legs.length; i++) { if (i === 0) { startLocation.latlng = legs[i].start_location; startLocation.address = legs[i].start_address; } endLocation.latlng = legs[i].end_location; endLocation.address = legs[i].end_address; var steps = legs[i].steps; for (j = 0; j < steps.length; j++) { var nextSegment = steps[j].path; for (k = 0; k < nextSegment.length; k++) { polyline.getPath().push(nextSegment[k]); bounds.extend(nextSegment[k]); } } } polyline.setMap(map); map.fitBounds(bounds); map.setZoom(18); } }); } DVD。假设您的代码已经编译好了,这应该可以正常工作 -

null

答案 1 :(得分:1)

您可以从问题中选择关键字,告诉您方法应该如何显示。

  

displayDVDsInCategory - 此方法应该有一个类别作为参数。它应该返回一个arrayList对象与指定类别中的所有DVD。如果给定类别中没有DVD,则arrayList的大小将为零

由于它告诉您返回DVD对象的ArrayList,因此方法的返回值应该是ArrayList,而不是单个DVD对象:

public ArrayList<DVD> displayDVDsInCategory(String category)   //return DVD list
{
    ArrayList<DVD> newList = new ArrayList<DVD>();
    for (DVD dvd : arraylist)    //for each dvd in your current list
        if(category.equalsIgnoreCase(dvd.getCategory())){     //if category matches
            newList.add(dvd);                                 //add to new list
    return newList;                                           //return DVD list
}

您可以创建一个新的空列表。在检查当前DVD列表时,如果其中任何一个与给定类别匹配,请将它们添加到新列表中。

最后,您将返回新列表而不是单个DVD对象。因此,如果没有DVD匹配,您仍然返回一个空的ArrayList。

答案 2 :(得分:0)

我不确定错误,但您的描述与您的代码不符。如果你想返回一个DVD列表,你的代码是错误的,它只返回第一张DVD类别与传递给params的类别匹配。

顺便说一句,如果您使用的是Java 8,则可以对代码进行一些改进。请参阅下面的代码段。

public static List<String> displayDVDsInCategory(final String category) {
    return arraylist.stream()
         .filter(obj -> obj.equalsIgnoreCase(category))
         .collect(Collectors.toList());
}