Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having trouble wrapping my head around a concept. I'm trying to convert a ArrayList(String) to a List (or List(String)), but I keep running into a cast exception. Yes I saw the code to convert a string array to a list, but I feel this a different scenario. my relevant code is here, please note the declarations are because I'm using GWT and thus have to declare some thigns as static and final...

private static List<String> values;
Ioma.dataservice.getPendingOrders(new AsyncCallback<ArrayList<Order>>(){
    @Override
    public void onFailure(Throwable caught) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onSuccess(ArrayList<Order> result) {
        ArrayList<String> orderNumbers = new ArrayList<String>();               
        //problem here, cannot cast arraylist<string> to list string for some reason? idk. SO it maybe?
        for(int i=0;i<result.size();i++){
            System.out.println("ordernumber: " + result.get(i).getOrderNumber());
            System.out.println("arraysize: " + result.size());
            orderNumbers.add(Integer.toString(result.get(i).getOrderNumber()));                 
        }
        for (int i=0; i<orderNumbers.size();i++){
            values.add(orderNumbers.get(i));
        }
        cellList.setRowData(values);
    }

});

basically I'm trying to create a cellList which wants a List as input. I get an arrayList of orders from a DB query and then iterate through to pull out order numbers which I then put into a arrayList, which I then want to convert to a List to use in cellList. Can someone explain why I can't do this and what the proper method should be?

share|improve this question
values doesn't seem to be initialized anywhere, you will get a NullPointerException before anything else. – Sotirios Delimanolis Apr 23 at 20:17
This code sample seems like it's missing things. – AHungerArtist Apr 23 at 20:21

closed as not a real question by Paul Bellora, SSR, Bill the Lizard Apr 27 at 15:04

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

5 Answers

up vote 1 down vote accepted

I don't understand the problem you have since an ArrayList implements a List with List being an interface.

You can change this line

ArrayList<String> orderNumbers = new ArrayList<String>(); 

to:

List<String> orderNumbers = new ArrayList<String>();
share|improve this answer
My issue was in my head I was thinking I had to convery the Arraylist to a List, I didn't realize that arraylist was a type List, I thought it was its own thing. I changed that line, and now it works. thank you. – john Apr 23 at 20:24
Personal recommendation, try to use the interface when declaring a variable so you can change the implementation afterwards. Imagine for example switching from ArrayList to a LinkedList, you wouldn't need to change anything if your methods expect a List. – Alex Apr 23 at 20:56
2  
@Alex - That's not good advice for a project that uses GWT, especially for GWT-RPC. From Google's developer guide: "To make the best use of polymorphism, however, you should still try to be as specific as your design allows when defining service interfaces. Increased specificity allows the compiler to do a better job of removing unnecessary code when it optimizes your application for size reduction." – Ted Hopp Apr 23 at 21:27
1  
I think that in general it is a good advice. Thank though for your link I definitely didn't know about it! – Alex Apr 23 at 22:53

An ArrayList is a List, so I'm not sure what might be happening. However, I do have a suspicion. Look at the imports at the top of the file. Perhaps you have the line atop the file:

import java.awt.List;

I have told my IDE not to use java.awt.List as a suggestion for List. However, you are still writing too much code. Consider Java's foreach loops:

@Override
public void onSuccess(ArrayList<Order> result) {
    List<String> orderNumbers = new ArrayList<String>();               
    for (Order order: result) {
        orderNumbers.add(Integer.toString(order.getOrderNumber()));                 
    }
    values.addAll(orderNumbers);
    cellList.setRowData(values);
}

But you don't need the orderNumbers variable at all. Just use values.add instead of orderNumbers.add. And then, why are you using a List<String> at all? Can you survive with a List<Integer> and avoid the conversion?

Whoops--you're using GWT. Does Google still advise programmers to not use interfaces so it doesn't have to generate every concrete implementation in Java?

share|improve this answer
thank you for the suggestions, I cleaned up my code. once i figured out that arraylist was a list itself. – john Apr 23 at 20:32

I don't see where you're trying to typecast anything. ArrayList<E> is a child of List<E>, meaning that an ArrayList is always a List, and all methods you might want to use from the ArrayList class (like size(), get(), indexOf(), etc.) are all available in List too! That's the beauty of inheritance :)

Can you give us a stack trace so we can see your exact problem.?

share|improve this answer

java.util.List is an interface, and the ArrayList<E> allready implementing it, you dont have to cast anything

share|improve this answer

An ArrayList<anything> is already a List<anything>, because ArrayList implements the List interface. No casting is necessary.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.