简体   繁体   中英

Java - Separate and display element from ArrayList like list with sub-list

I have a car class with attributes Manufacturer and Model . A Manufacturer can have many Model , for example manufacturer Chevrolet have models such as Camaro , Malibu ... I create an ArrayList to store them, something like this:

Cars objCar;
ArrayList<Cars> arrListCars;

objCar = new Cars("Chevrolet","Camaro");
arrListCars.add(objCar);
objCar = new Cars("Chevrolet","Malibu");
arrListCars.add(objCar);
objCar = new Cars("Ford","Mustang");
arrListCars.add(objCar);
objCar = new Cars("Ford","Fiesta");
arrListCars.add(objCar);

I can print and display a list like:

Manufacturer: Chevrolet | Model: Camaro
Manufacturer: Chevrolet | Model: Malibu
Manufacturer: Ford | Model: Mustang
Manufacturer: Ford | Model: Fiesta

But I want to display something like this:

Manufacturer: Chevrolet | Model: Camaro Malibu
Manufacturer: Ford | Model: Mustang Fiesta

I have getter setter in the car class.

Please help, I'm new to java and really don't know how to pick out element like that, thanks

So one way you can do this is by using getters. I assume that in your Cars class you probably have the variables Manufacturer and Model. Strings. As such I assume it is

private String manufacturer;
private String model

public Cars(String manufacturer, String model) {
this.manufacturer = manu;
this.model = model;
}

Now, within that class you can create getters.

public String getManufacturer() {
return manufacturer;
}

public String getModel() {
return model;
} 

So now you have access to your inner data variables.

Inside your main code, after you have added your cars to the list you can access them (and print them in the format you want as follows:

for(Car cars: arrListCars) {
System.out.println("Manufacturer: " + cars.getManufacturer() + "| Model: " + cars.getModel());
}

The above for loop is a for-each example. Another way to use the for loop, in a more traditional way that you may be familiar with is the indexed for loop. Ex:

for(int i = 0; i < arrListCars.size(); i++) {
System.out.println("Manufacturer: " + arrListCars.get(i).getManufacturer() + "| Model: " + arrListCars.get(i).getModel());
}

EDIT

I see, so you want to make it unified. Okay, for that you will need to use a Map. Quick explanation of Maps. Maps map Object Keys to Object Values, usually shown as Map in the IDE (say like Eclipse). As such in your code you will need to add those values to the Map.

Ex:

Map carMap = new HashMap<>();

carMap.put("Ford","Mustang"); carMap.put("Chevy,"Tahoe");

Then within your code you can create an intermediate String. Ex:

String chevyManu = "Manufacturer: Chevrolet | ";
String fordManu = "Manufacturer: Ford | ";

for (String key : carMap.keySet()) {
   if(key.equalsIgnoreCase("Ford") {
       fordManu += carMap.get(key);
   }
   else if(key.equalsIgnoreCase("Chevrolet") {
             chevyManu += carMap.get(key);
   }
}

I had to look up how to iterate over a map, I used http://javarevisited.blogspot.com/2011/12/how-to-traverse-or-loop-hashmap-in-java.html as my source.

EDIT 2

Thinking over it, I think you can actually avoid using Maps as well. We will still use the idea of having the two Strings (chevyManu and fordManu) and first initialize them to "Manufacturer: Chevrolet or Ford | ", then inside the code that loops over the array we can create some basic if statements.

Ex:

for(Car cars: arrListCars) {
if(cars.getManufacturer().equalsIgnoreCase("Chevrolet") {
chevyManu += cars.getModel() + " ";
}
else if(cars.getManufacturer().equalsIgnoreCase("Ford") {
fordManu += cars.getModel() + " ";
}
}

Idea here being to leverage what you already have and try to avoid added complexity. I hope this helped :)

I have some untested code that might help:

public void getAllCarsWithManufacturer(String manufacturer, List<Cars> cars, List<Cars> out){
    for (Cars car : cars){
        // make sure manufacturer matches the one that we need
        if (car.getManufacturer().equals(manufacturer)){
            // add car to output list
            out.add(car);
        }
    }
}

public void printOutAllCarsWithManufacturer(String manufacturer, List<Cars> library, List<Cars> out){
    // empty the list we are using to output the info
    out.clear();
    // fill the out with the correct manufacturer
    getAllCarsWithManufacturer(manufacturer, library, out);
    // print out the data
    System.out.println("manufacturer: "+manufacturer + " | " + out.toString());
}

public void printOutAllCars(List<Cars> cars){
    // sets cant have duplicates
    HashSet<String> nodupcars = new HashSet<String>();
    List<Cars> out = new ArrayList<Cars>();
    // use the nodupcars to watch manufacturers
    for (Cars car : cars){
        nodupcars.add(car.getManufacturer());
    }
    for (String car : nodupcars){
        printOutAllCarsWithManufacturer(car, cars, out);
    }
}

You would use printOutAllCars() to show all the cars based on manufacturer. This is also assuming that Cars has some sort of getManufacturer() method to retrieve the manufacturer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM