Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert ArrayList<String> to java.util.List [closed]

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 things 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?

like image 981
john Avatar asked Apr 23 '13 20:04

john


1 Answers

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>();
like image 79
Alex Avatar answered Oct 11 '22 19:10

Alex