12

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?

shmosel
  • 49,289
  • 6
  • 73
  • 138
john
  • 775
  • 3
  • 15
  • 31
  • `values` doesn't seem to be initialized anywhere, you will get a `NullPointerException` before anything else. – Sotirios Delimanolis Apr 23 '13 at 20:17
  • This code sample seems like it's missing things. – AHungerArtist Apr 23 '13 at 20:21
  • I don't see where you're trying to typecast anything. `ArrayList` is a child of `List`, 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.? – mdierker Apr 23 '13 at 20:17

4 Answers4

25

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>();
Alex
  • 1,066
  • 9
  • 13
  • 1
    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 '13 at 20:56
  • 4
    @Alex - That's not good advice for a project that uses GWT, especially for GWT-RPC. From [Google's developer guide](https://developers.google.com/web-toolkit/doc/latest/DevGuideServerCommunication#DevGuideRemoteProcedureCalls): "_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 '13 at 21:27
  • 1
    I think that in general it is a [good advice](http://stackoverflow.com/questions/9852831/polymorphism-why-use-list-list-new-arraylist-instead-of-arraylist-list-n). Thank though for your link I definitely didn't know about it! – Alex Apr 23 '13 at 22:53
  • I don't understand the problem either, but Eclipse has problems with something like `List> whatever () {List> result=null;return result;}` I hate Java type system for this kind of things... – Trylks Nov 05 '14 at 15:34
5

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

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

java.util.List is an interface, and the ArrayList<E> already implemented it, you don't have to cast anything

jfly
  • 7,715
  • 3
  • 35
  • 65
Dima
  • 8,586
  • 4
  • 28
  • 57
0

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?

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29