1 2 3 Previous Next 42 Replies Latest reply: Dec 10, 2008 10:13 PM by 843785 RSS

    ArrayList help

    843785
      Hi, I need to make a class for homework that uses ArrayLists but I am stuck on a method where we have to reverse all the items inside. The program will compile but whenever I run it the program has an error can anyone help?
      import java.util.*;
      
      public class Purse
      {
           ArrayList<String> purse;
           
           public Purse()
           {
                purse = new ArrayList<String>();
           
           }
           
           public void addCoin(String coinName)
           {
                purse.add(coinName);
           }
           
           public String toString()
           {     
                String out = "]";
                String space = ",";
                String start = "Purse[";
                
                System.out.print(start);
                
                for(int i = 0;i < purse.size(); i++)
                {
                     System.out.print(purse.get(i));
                     if(i!=purse.size()-1)
                          System.out.print(space);
                }
                
                System.out.print(out);
                System.out.println();
                System.out.println("The array size is: " + purse.size());
                return out;
           }     
           
           public void reverse()
           {
                int a = purse.size();
                for(int i = 0; i < a; i++)
                {
                     String temp = purse.get(i);
                     purse.set(i, purse.get(a));
                     purse.set(a, temp);
                     a--;
                }
           }
        1 2 3 Previous Next