Results 1 to 5 of 5
  1. #1
    Psygnosys is offline Overboard!
    Join Date
    Apr 2012
    Location
    Manchester/Aberystwyth, UK
    Posts
    4
    Rep Power
    0

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


    Java 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;

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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)

  3. #3
    Psygnosys is offline Overboard!
    Join Date
    Apr 2012
    Location
    Manchester/Aberystwyth, UK
    Posts
    4
    Rep Power
    0

    Default Re: Converting ArrayList<Object> into a String[] of their toString()s

    Quote Originally Posted by doWhile View Post
    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.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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.

  5. #5
    Psygnosys is offline Overboard!
    Join Date
    Apr 2012
    Location
    Manchester/Aberystwyth, UK
    Posts
    4
    Rep Power
    0

    Default 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.

    Java Code:
    public String[] ordersArray(){
    		String[] ordersArray=new String[orders.size()];
    		int i=0;
    		for(Order currentOrder: orders){
    			ordersArray[i]=currentOrder.toString();
    			i++;
    		}
    		return ordersArray;
    	}

Similar Threads

  1. Converting Arraylist to String array
    By ankit1801 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 11:07 AM
  2. error with String toString()
    By b.m in forum New To Java
    Replies: 9
    Last Post: 12-16-2010, 03:02 PM
  3. Converting ArrayList to Array
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 03:34 PM
  4. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 04:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •