Converting ArrayList<Object> into a String[] of their toString()s
I'm trying to track down a problem & am wondering if anyone can spot something inherently wrong with this method?
Code:
private ArrayList<Order> orders=new ArrayList();
public String[] ordersArray(){
String[] ordersArray=new String[orders.size()];
for(int i=0;i==orders.size();i++){
ordersArray[i]=currentOrder.toString();
}
return ordersArray;
Re: Converting ArrayList<Object> into a String[] of their toString()s
To turn your question around, what do YOU think the problem is? Are there exceptions? Does it compile? When you say 'track down a problem'...what is the problem? (btw, there are quite a few problems within)
Re: Converting ArrayList<Object> into a String[] of their toString()s
Quote:
Originally Posted by
doWhile
To turn your question around, what do YOU think the problem is? Are there exceptions? Does it compile? When you say 'track down a problem'...what is the problem? (btw, there are quite a few problems within)
I'm using it to convert an ArrayList into an array of Strings so it can be used in a JList. It does compile & I've tried testing this method with a few instances of Order, but it does some weird things in the JList I've used it in. I was wondering if the problem was in my testing, or in this method itself. Tried using the debugger a bit, to not much avail though.
Re: Converting ArrayList<Object> into a String[] of their toString()s
Never hurts to start at the basics. See the following
The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
And notice the syntax of a for loop (eg initialization;termination;increment)
..and also note that the ArrayList class has a method for converting to arrays.
Re: Converting ArrayList<Object> into a String[] of their toString()s
I looked at the loop, & changed my mind about it - using a for/each loop now, which works. I looked into that toArray() method, but wasn't sure how to use it. Thanks for the advice though.
Code:
public String[] ordersArray(){
String[] ordersArray=new String[orders.size()];
int i=0;
for(Order currentOrder: orders){
ordersArray[i]=currentOrder.toString();
i++;
}
return ordersArray;
}