Hi. I have a little problem with my code. I want to cast a complete arrayList to a different type, but java wont let me... Here is a compressed version of what is happening:

//The interface I use.
public abstract interface Drawable{

//some methods
}

//A class that is supposed to be drawable
public abstract class pObject implements Drawable{

//Methods

}

//A class that works magic on the objects
public class engine{

ArrayList<pObject> obs = new...;


//Method returning the pObjecs, that is drawable
public ArrayList<pObject> getList(){

return obs;
}

}


//The rendering work horse 
public class drawer{

arrayList<Drawable> draw =  new...;


//The method that should add drawable objects to the rendering stack
public void addArrayList(ArrayList<Drawable> i)
{
draw.addAll(i);
}

}


//A random class that binds everything together
public class someThing{

engine en = new  ...;

drawer drawer1 = new..;

public void addObjects(){


//Finally, here is the issue: I get error saying the list is not able to be cast
// to a drawable type

drawer1.addObjects((ArrayList<Drawable>)en.getList());

}


}

Soooo.. What is the issue here? Is it simply syntax or something else?

Of course, I can just loop through them. But this is not, well, "a beautiful way of doing it".

Can you make a small,complete, executable example vs what you've posted with the ...

I could, but other than copying my whole code, that would take too long for me:p The stange thing is that i can parse one and one of them, but not the complete arraylist. Maybe I'm doing it wrong? Is there a special way of parsing ArrayLists?

What do you mean by "parsing" an ArrayList?
An ArrayList is a container that holds objects and has a set of methods allowing access to those objects.

I want to "cast" the arrayList to another type. Like ArrayList<object1> to ArrayList<object2> where object1 inheres from object2. (object types of course). So, something like this:

public class one{
//This would be "object2" from above.
}

public class two extends one{
//something. 
}

public static void main(String[] args){
ArrayList<two> a = new ...;
ArrayList<one> b;

//Here is where it gets scetcy:
b = (one)a;
//Basicly this is my issue. How to cast it into another type?

}

I have of course tried googeling all of this, but have yet to find any good answers.

> b = (one)a;

`b` and `a` are of reference type ArrayList but you are trying to cast them to type `one` and hence the error.

BTW, instead of posting large code snippets it would be better if you explained "what" you wanted to do and a small code snippet which demonstrates the exact problem you are facing. Read SSCCE.

> b = (one)a;

`b` and `a` are of reference type ArrayList but you are trying to cast them to type `one` and hence the error.

I know this does not work, but I hoped you would understand what I was trying to do. As I said: "Here is where it is getting sketchy".

BTW, instead of posting large code snippets it would be better if you explained "what" you wanted to do and a small code snippet which demonstrates the exact problem you are facing. Read SSCCE.

No offense, but if you had read the posts over I did indeed give a short explanation of what I was having issues with. I do agree that my first code was a bit long, but the second one is as short as I can manage to make it.

Anyways. Is there anyone who know how to cast an ArrayList into another arraylist with different different datatype? (also the datatypes inherit from each other as showed in some of my previous posts.

Thanks:)

No offense, but if you had read the posts over I did indeed give a short explanation of what I was having issues with. I do agree that my first code was a bit long, but the second one is as short as I can manage to make it.

No offense, but did you even read the link I posted? The second 'C' in SSCCE stands for compilable code which none of your posts contain. When posting queries/issues, try to phrase the question such that the person trying to help you out has to exert the least effort. Things like "i was hoping you'd understand" wastes my as well as your time. Anyways, after trying really hard to make out what you want, here's what I think.

You can't cast Arraylists the way you are trying to do. This is because generics in Java don't exhibit the covariance property shown by arrays. When dealing with arrays, T[] can be assigned to S[] if S is the super-type of T. The problem in this case is that you might end up putting elements in an array of a type which don't belong to that array which blows up at runtime (not compile time).

Integer[] ints = new Integer[] { 1, 2 };
Number[] nums = ints; // ok
nums[1] = 1.23;  // runtime exception

Generics on the other hand are invariant i.e. List<T> can't be assigned to List<S> unless both S and T are the same. Covariance can be introduced in this case by using wildcards.

List<Integer> ints = new ArrayList<Integer>();
List<Number> nums = ints; // error
List<? extends Number> nums = ints; // ok

Then again, there's a catch here. Since we say that `nums` is a list which can contain Number type elements, you can't add to this list using `nums` reference. This is because the underlying list might be of integers and you might try to add a float to it.

List<Integer> ints = new ArrayList<Integer>();
List<? extends Number> nums = ints; // ok
nums.add(3.23); // error and rightfully so

Since the code you posted in your first post is just adding objects, you can use the wildcards and still be OK with it since all you'd be doing is getting the elements.

class Drawer {
  private List<Drawable> toDrawObjects = new ArrayList<Drawable>();

  public void addObjects(List<? extends Drawable> list) {
    for(Drawable d : list) {
      toDrawObjects.add(d);      
    }
  }
}

Arrays.asList(drawableRecords.toArray(new pObject[0]));

i have a same doubt mate ....i dont waana waste much time on that i created a method to to type cast a list..

I think that sos's point was that you shouldn't have to perform that kind of cast. It probably results from a bad design decision.
The "correct" solution is to declare the List(s) appropriately with a ? extends ...

In this particular case the Lists on lines 17 and 21 shoud be declared as ArrayList<? extends Drawable> so the intent is perfectly obvious, and no cast is needed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.