LinkedList的遍历、搜索及打印实现相关技术问询
Hey there! Let's break down how to implement print functionality, traversal, and search operations for your cinema class's LinkedList step by step. I'll build off the code snippet you provided.
First, we need a way to display the details of each cinema object clearly. Add an toString() override to your cinema class—this lets Java know how to format the object when we print it:
@Override public String toString() { return "Cinema Hall [ID: " + cinemaNum + ", Row: " + row + ", Total Seats: " + numSeats + "]"; }
Then, create a static method to print the entire cinList linked list. You can use a few different approaches here:
Option 1: Enhanced For Loop (Most Readable)
public static void printCinemaHalls() { if (cinList.isEmpty()) { System.out.println("No cinema halls have been added yet."); return; } System.out.println("=== All Cinema Halls ==="); for (cinema hall : cinList) { System.out.println(hall); // Automatically uses our toString() method } }
Option 2: Lambda + forEach (Concise Java 8+ Style)
public static void printCinemaHalls() { if (cinList.isEmpty()) { System.out.println("No cinema halls available."); return; } System.out.println("=== All Cinema Halls ==="); cinList.forEach(System.out::println); }
Traversal means iterating through each element in the list. Here are the most common methods:
Enhanced For Loop (Best for Simple Iteration)
Great when you just need to read or process each element without modifying the list structure:
for (cinema hall : cinList) { // Example: Print each hall's seat count System.out.println("Hall " + hall.cinemaNum + " has " + hall.numSeats + " seats"); }
Iterator (Safe for Modifying the List Mid-Traversal)
Use this if you need to remove elements while iterating (avoids ConcurrentModificationException):
import java.util.Iterator; // ... Iterator<cinema> hallIterator = cinList.iterator(); while (hallIterator.hasNext()) { cinema currentHall = hallIterator.next(); // Example: Remove halls with zero seats if (currentHall.numSeats == 0) { hallIterator.remove(); System.out.println("Removed empty hall ID: " + currentHall.cinemaNum); } }
Index-Based Loop (For Accessing by Position)
Note: LinkedList's get(index) is O(n) time complexity (slower than ArrayList for frequent index access), so use this sparingly:
for (int i = 0; i < cinList.size(); i++) { cinema hall = cinList.get(i); System.out.println("Position " + i + ": " + hall); }
Search lets you find elements matching specific criteria. Here are practical examples for your use case:
Search by Cinema Number (Return First Match)
public static cinema searchByCinemaId(int targetId) { for (cinema hall : cinList) { if (hall.cinemaNum == targetId) { return hall; // Return the first matching hall } } return null; // Return null if no match is found }
How to use it:
cinema foundHall = cinema.searchByCinemaId(3); if (foundHall != null) { System.out.println("Found Hall: " + foundHall); } else { System.out.println("No hall with ID 3 exists."); }
Search by Row Name (Return All Matches)
Since multiple halls might share the same row, we'll return a list of matches:
import java.util.ArrayList; import java.util.List; // ... public static List<cinema> searchByRowName(String targetRow) { List<cinema> matchingHalls = new ArrayList<>(); for (cinema hall : cinList) { // Use equalsIgnoreCase for case-insensitive matching (e.g., "A" matches "a") if (hall.row.equalsIgnoreCase(targetRow)) { matchingHalls.add(hall); } } return matchingHalls; }
How to use it:
List<cinema> rowAHalls = cinema.searchByRowName("A"); if (!rowAHalls.isEmpty()) { System.out.println("=== Halls in Row A ==="); rowAHalls.forEach(System.out::println); } else { System.out.println("No halls found in Row A."); }
Java 8+ Stream API (Concise Search)
If you're using Java 8 or later, streams make search even cleaner:
// Find the first hall with 50 seats java.util.Optional<cinema> fiftySeatHall = cinList.stream() .filter(hall -> hall.numSeats == 50) .findFirst(); // Print the result if it exists fiftySeatHall.ifPresent(hall -> System.out.println("Found 50-seat hall: " + hall));
addHalls Method Looks like your addHalls method was cut off—here's the complete version to add halls to your linked list:
public static void addHalls(cinemaValues cin) { cinema newHall = new cinema(cin.getCinemaNumber(), cin.getRowName(), cin.getNumSeats()); cinList.add(newHall); // Add the new hall to the linked list }
内容的提问来源于stack exchange,提问作者Utkarsh Atri

