Java enhanced for VS.NET foreach loop

I have a question that I could not find the answer to. Let's say we have the following code in java or C #:

class Car {
   /* car stuff */
}

And then in Java

class Truck extends Car {
   /* truck stuff */
}

and c #

class Truck : Car {
   /* truck stuff again */
}

In C #, the following works fine:

List<Car> carList = new List<Car>();
//add some objects to the collection
foreach(Truck t in carList)
     //do stuff with only the Truck objects in the carList collection

This works because Truck is a subclass of Car, which in simple terms means that each Truck is also a car. The fact is that a check of this type is performed, and only cars are selected from carList.

If we try the same thing in Java:

List<Car> carList = new ArrayList<Car>();
//add some objects to the collection
for(Truck t : carList)
     //**PROBLEM**

Because of the code inside the extended loop, the code does not even compile. Instead, we should do something similar to get the same effect:

for(Car t : carList)
   if(t instanceof Car)
      //cast t to Truck and do truck stuff with it

This is the same idea that works without problems in C #, but in Java you need additional code. Even the syntax is almost the same! Is there a reason why this does not work in Java?

+5
4

, , carList.

, . -, Truck s, # . , #,

foreach(Truck t in carList) {
    ...
}

foreach(object _t in carList) {
    Truck t = (Truck)_t;        // throws an InvalidCastException if _t is not a Truck
    ...
}

Java, , : .


, Java # -? :

# foreach, . , a List<Car>. # Java foreach,

foreach(object _c in myArraylistContainingOnlyCars) {
    Car c = (Car)_c;
    // do something with c
}

. , generics Java (Java 5), ​​ .

+7

#,

foreach(Truck t in carList)

, ():

var enumerator = carList.GetEnumerator();

while (enumerator.MoveNext())
{
    Truck t = (Truck)enumerator.Current;
    // do stuff
}

, carList .

, :

namespace ConsoleApplication1
{
    class Car
    {
    }

    class Truck: Car
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            Car[] cars = new[] { new Car(), new Truck() };

            foreach (Truck t in cars)
            {
                Console.WriteLine("1 truck");
            }

            Console.Read();
        }
    }
}

# java -, . , " , , " #. , , #, java.

, #, linq , :

foreach (Truck t in carList.OfType<Truck>())
{
    //do stuff
}

, .

+4
List<Car> carList = new ArrayList<Car>();
//add some objects to the collection
for(Truck t : carList)
     //**PROBLEM**

Java Generics, Java 5, .NET.

- - . , - /

Truck Car. ( ), ( ),

+2

, Truck Car.

for(Car t : carList) {
    if(t instanceof Car)
        // Do stuff here
}

:

for(Car t : carList) {
    // Do stuff here
}

, :

List<Truck> truckList = new ArrayList<Truck>();

for(Car car : truckList) {
    // Do stuff here
}

:

List<Car> carList = new ArrayList<Car>();

for (Iterator<Car> it = carList.iterator(); it.hasNext();) {
    Truck car = (Truck) it.next();
    // Do stuff here
}

:

List<Car> carList = new ArrayList<Car>();

for(Truck truck : carList) {
    // Do stuff here
}
+1

All Articles