Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Method parseIntArrayList

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Method parseIntArrayList

    This code was originally for lisp lists, i modifed it for ArrayLists. But it doesn't work properly. It supposed to take a string ( a list of numbers), convert them to integers and return an ArrayList
    public static ArrayList<Integer> parseIntArrayList(String str)
     {
      String line = str.trim();
      String contents = line.substring(1,line.length()-1).trim();
      if(contents.length()==0)
         return null;
      String[] nums = contents.split(",");
      ArrayList<Integer> list = null;
      for(int i=nums.length-1; i>=0; i--)
          {
           String num = nums[i].trim();
           list.add(i,Integer.parseInt(num));
          }
      return list;
     }
    Last edited by helloworld922; November 25th, 2009 at 04:14 PM. Reason: Please use [code] tags!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Method parseIntArrayList

    You need to create a new ArrayList rather than initializing it to null aka ArrayList<Integer> list = new ArrayList<Integer>().