Java 将arraylist传递给方法

Java 将arraylist传递给方法,java,arraylist,Java,Arraylist,在准备即将到来的考试时,我遇到了这个问题。问题1作为代码中的注释 public class List { public static void main(String[] args) { ArrayList<Song> songList = new ArrayList<Song>(); } public void addSong(Song song) { //Q.1 implement the addSong

在准备即将到来的考试时,我遇到了这个问题。问题1作为代码中的注释

public class List {    

    public static void main(String[] args) {
        ArrayList<Song> songList = new ArrayList<Song>();
    }

    public void addSong(Song song) {
      //Q.1 implement the addSong method which adds a Song object to the cart
        Song aSong = new Song("A Song", 20);

    }

    public void getTotalSongs() {
       //Q.2 implement the getTotalSongs that returns the total number of songs in the

    }

    public void displayAllSongs() {
        //Q.3 implement the displayAllSongs method which prints all song titles in the cart

    }

    public double getTotalPurchuse() {

        for (List aList : songList.Song.getPrice()) {
            return aList;
        }

    class Song {

        private String title;
        private double price;

        Song(String title, double price) {
            this.title = title;
            this.price = price;
        }

        public String getTitle() {
            return title;
        }

        public double getPrice() {
            return price;
        }
    }
}
到目前为止,我只尝试了问题1,但其他问题都不是问题

我在做问题要求的事情吗?i、 e.Song aSong=新歌曲A歌曲,20;。我觉得这太简单了,我应该使用ArrayName.Add;。如果是这种情况,如何将ArrayList解析为另一个方法?注意,我不能更改已经存在的代码,我只能添加自己的代码,因为这是一项考试。

您应该将songList移动到类范围,并使用add方法songList.addsong;添加歌曲列表


这应该是你问题的答案

  public class List {    
        private List<Song> songList = new ArrayList<Song>();

        public static void main(String[] args) {
            List cart = new List();
            Song song = new Song("song", 3.4);
            cart.addsong(song);
            cart.displayAllSongs();
        }

        public void addSong(Song song) {
           //Q.1 implement the addSong method which adds a Song object to the cart
           songList.add(song);
        }

        public List<Song> getTotalSongs() {
           //Q.2 implement the getTotalSongs that returns the total number of songs in the
           return songList;
        }

        public void displayAllSongs() {
            //Q.3 implement the displayAllSongs method which prints all song titles in the cart
            System.out.println("List of all songs: " + songList);
        }

        public double getTotalPurchuse() {
            //Q.4 implement the getTotalPurchuse method which returns the total purhucse for all songs in the cart
            Iterator<Song> it = songList.iterator();
            double total = 0;
            while(it.hasNext()){
                Song next = it.next();    
                total += next.getPrice();
            }
            return total;
        }

        class Song {

            private String title;
            private double price;

            Song(String title, double price) {
                this.title = title;
                this.price = price;
            }

            public String getTitle() {
                return title;
            }

            public double getPrice() {
                return price;
            }
        }
    }

您不应该在该方法中创建新的Song对象,您的songList不应该是类中的实例变量吗?您的数组应该声明为字段,而不是在主方法中,而且addSong方法不正确,您应该像这样将歌曲添加到arrayList中:songList.addSong;将songList移到main之外,并在addSongSong中执行songList.adds;如题中所述,我无法更改代码,这是过去考试中的一个给定示例。我所能做的就是向方法中添加代码,因此我无法删除任何主要内容。然后,这两个问题都无法解决。您将无法访问任何给定方法中的歌曲列表,因此问题中的代码有问题,或者您误解了问题。编辑我只是注意到,对于第二季度,你应该用返回void的方法返回歌曲的数量,所以这个问题写得太差了,他不想更改骨架,但如果方法规范中有返回歌曲列表的方法,则该方法不能无效。实际上,OP发布的代码是错误的,但是如果仔细阅读注释,getTotalSongs应该返回int。你还应该添加一个描述,说明你答案中的代码是做什么的,以及它是如何解决OP遇到的问题的。我试过了,它得到了一个非静态变量,不能从静态上下文中引用Song aSong=new SongA Song,20;你把课堂歌曲移到列表之外了吗?好的,在考试的细节中,它说在指定的位置添加你的代码。不允许在类实现中的其他位置添加其他代码。例如,不允许在addSong方法之前添加任何代码。这意味着我无法创建您放置的对象:
public class Song {

        private String title;
        private double price;

        Song(String title, double price) {
            this.title = title;
            this.price = price;
        }

        public String getTitle() {
            return title;
        }

        public double getPrice() {
            return price;
        }
    }
  public class List {    
        private List<Song> songList = new ArrayList<Song>();

        public static void main(String[] args) {
            List cart = new List();
            Song song = new Song("song", 3.4);
            cart.addsong(song);
            cart.displayAllSongs();
        }

        public void addSong(Song song) {
           //Q.1 implement the addSong method which adds a Song object to the cart
           songList.add(song);
        }

        public List<Song> getTotalSongs() {
           //Q.2 implement the getTotalSongs that returns the total number of songs in the
           return songList;
        }

        public void displayAllSongs() {
            //Q.3 implement the displayAllSongs method which prints all song titles in the cart
            System.out.println("List of all songs: " + songList);
        }

        public double getTotalPurchuse() {
            //Q.4 implement the getTotalPurchuse method which returns the total purhucse for all songs in the cart
            Iterator<Song> it = songList.iterator();
            double total = 0;
            while(it.hasNext()){
                Song next = it.next();    
                total += next.getPrice();
            }
            return total;
        }

        class Song {

            private String title;
            private double price;

            Song(String title, double price) {
                this.title = title;
                this.price = price;
            }

            public String getTitle() {
                return title;
            }

            public double getPrice() {
                return price;
            }
        }
    }