-3

So my teacher assigned me a problem that I have to put the output of an array and pipe(not sure if that's the right word) the array values into the arraylist and then reverse the order. I know I'm very close and just missing something quite obvious. Every time I ask the teacher he just confuses me even more. Thank you for your help. I don't know what I would do without this site. My teacher said we need to have two separate classes. Purse.Java and TestPurse.Java. The array is in the TestPurse class and the arraylist is in the Purse class. Here is my testpurse.java code:

 public class Purse   {
    // TODO Auto-generated constructor stub

    /**
      * @param args
    */
    public static void main(String[] args) {
       Purse coin = new Purse();

       coin.addcoin ("Quarter");
       coin.addcoin ("Dime");
       coin.addcoin ("Nickel");
       coin.addcoin ("Dime");
       System.out.println (coin);
    }

    public void addcoin(String string) {
    // TODO Auto-generated method stub
       ArrayList<String> newpurse = new ArrayList<String>();

      Collections.reverse(newpurse);
      System.out.println (newpurse);
    }

}
4

3 回答 3

1

好吧,这是从数组创建 ArrayList 的方法:

从数组创建 ArrayList

这就是您颠倒 ArrayList 中项目顺序的方式:

以相反的顺序排序列表

于 2013-09-11T20:02:02.937 回答
1

可能这就是你想要的

  public class PurseTest {

         public static void main(String[] args) {
             String[] coin = { "Quarter", "Dime", "Nickel", "Dime" };
             Purse purse = new Purse();
             purse.addAllCoin(coin);

            System.out.println(purse);
         }

   }

   public class Purse {
            ArrayList<String> data;

            public Purse() {
              data = new ArrayList<String>();

            }

           public void addAllCoin(String[] strArr) {

              for (String s : strArr) {
                  data.add(s);
              }

            }

           @Override
           public String toString() {
               Collections.reverse(data);
               return data.toString();
           }

   }

你能试试这个吗

于 2013-09-11T20:28:50.480 回答
0

更新代码

import java.util.ArrayList;
import java.util.Collections;

class Purse   
{
    ArrayList<String> newPurse= new ArrayList<String>();   

    public void addcoin(String string) 
    {           
       newPurse.add(string);          
    }
    @Override
    public String toString()
    {
        return newPurse.toString();
    }
    public String reverse()
    {
       ArrayList<String> al = new ArrayList<String>(newPurse);//making copy of newPurse so that reversing won't affect on it directly.
       Collections.reverse(al);
       return al.toString();
    }

}
public class TestPurse
{
    public static void main(String[] args) 
    {
       Purse coin = new Purse();    
       coin.addcoin ("Quarter");
       coin.addcoin ("Dime");
       coin.addcoin ("Nickel");
       coin.addcoin ("Dime");

       System.out.println (coin.toString());
       System.out.println (coin.reverse());
    }
}
于 2013-09-11T20:16:20.800 回答