'Accessing lists from one class to another class
I am a beginner Java programmer. I am trying to access two lists from the class Car in the class Traffic, so I can perform a while loop that would loop till the lists from the main class are empty
This is the code I have now, I tried extending Traffic from the class car, but that didn't work and I am stuck. How can I solve this?
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class Car{
static String name;
public Object waiting_cars;
Car(String n) {
name = n;
}
public static void main(String[] args) {
List<Car> waiting_cars = new ArrayList<Car>();
List<Car> after_light = new ArrayList<Car>();)
for(int i =0; waiting_cars.size()<= 99; i++){
Car carname = new Car(name);
waiting_cars.add(carname); //CarClass@4617c264
}
System.out.println(waiting_cars.size());
}
}
class Traffic{
void trafficLights(){
while(!waiting_cars.size().equals(0)){ //WHERE THE PROBLEM OCCURS
}
}
}
Solution 1:[1]
As a beginner in Java (and probably, OOP as well), a lot of the fundamentals of Java have not yet settled in. So, you came up with an inadequate modelling of the problem domain.
A key idea of OOP is that data isn't stored in some globally available variables, but as private information within some class instance. And if you need to know that data, you have to kindly ask the instance for the information (by calling a method where the information is returned). And if the class doesn't have such a method, then the information is considered private and only visible to the instance itself that owns the field.
Let's have a look at your code now (I took the freedom to indent your code consistently, something that your IDE or Java-enabled editor can do automatically):
public class Car{
This class should contain the properties and functions of a single car (at least that's what everybody will expect from its name).
static String name;
By using static, you declare that all cars share exactly one name. Having individual names for cars (e.g. from the license plates) would be more plausible: omit the static keyword.
public Object waiting_cars;
Hereby you declare every Car to have waiting_cars - quite surprising, what does that mean that one car has waiting cars? This information should not be kept in the Car class (see below). And you should declare the data type you want to use for waiting_cars, e.g. List<Car> instead of Object.
Car(String n) {
name = n;
}
This defines a constructor, allowing you to supply a name for the car, by e.g. calling new Car("XYZ-123");. If you changed the static String name; to omit the static keyword, the constructor would become absolutely valid.
public static void main(String[] args) {
List<Car> waiting_cars = new ArrayList<Car>();
List<Car> after_light = new ArrayList<Car>();)
for(int i =0; waiting_cars.size()<= 99; i++){
Car carname = new Car(name);
The line above is nonsense (and will no longer even compile if you removed the static keyword). As it stands now, it takes the value of the static field name, supplies that to the Car constructor, and this constructor stores it into the static field name, exactly where it came from. And the variable name carname is misleading, as it sounds like a string naming a Car instead of what it is, a Car. A more plausible line would be Car car = new Car("Car-" + i);, giving names like "Car-0", "Car-1" and so on to the cars.
waiting_cars.add(carname); //CarClass@4617c264
}
System.out.println(waiting_cars.size());
}
}
The problem you describe comes from your modelling. You want to deal with traffic lights, and a better modelling would associate the waiting cars and those that passed with one traffic light instead of making them part of a Car.
So, you could create a class TrafficLight with fields waitingCars and carsPassed. As cars can arrive at the traffic light, and pass the trafficLight, two methods come to mind: arrive(Car car) and passNextCar(). Something like
public class TrafficLight {
private List<Car> waitingCars = new ArrayList<>();
private List<Car> carsPassed = new ArrayList<>();
public void arrive(Car car) {
waitingCars.add(car);
}
public void passNextCar() {
if (!waitingCars.isEmpty()) {
Car car = waitingCars.get(0);
waitingCars.remove(0);
carsPassed.add(car);
}
}
}
Right now, waitingCars and carsPassed are private and invisible to the outside world. If some other class needs to know about the cars that are waiting at or have passed a given traffic light, this other class will ask the traffic light for that information, by calling a method like trafficLight.getNumberWaiting(). Of course, such a method has exist in the TrafficLight class first, e.g.
public int getNumberWaiting() {
return waitingCars.size();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ralf Kleberhoff |
