将数组列表传递给方法

时间:2019-06-17 10:35:43

标签: java arraylist

在为我即将参加的考试而学习时,我遇到了这个问题。 (问题1作为代码中的注释)

df.loc[df.sum(1).argsort()]
# Use @jezraels answer if the index is not range(len(df.index))

   0   1
1  1   2
2  2   3
0  1  10

(到目前为止,我仅尝试了问题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; } } } )。我觉得这太简单了,应该使用Song aSong = new Song("A Song", 20);。如果是这种情况,一个人如何将ArrayName.Add();解析为另一种方法? (请注意,我无法更改已经存在的内容,因为这是考试,所以只能添加自己的代码)。

2 个答案:

答案 0 :(得分:1)

您应将songList移至类范围,并使用添加方法songList.add(song);添加songList

public class List {    
ArrayList<Song> songList = new ArrayList<Song>();
public static void main(String[] args) {
    Song aSong = new Song("A Song", 20);
    List test = new List();
    test.addSong(aSong);
    //check total song
    test.getTotalSongs(); //print out 1
}

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

    songList.add(song);
}

public void getTotalSongs() {
   //Q.2 implement the getTotalSongs that returns the total number of songs in the
   System.out.println(songList.size());

}

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

}

public double getTotalPurchuse() {
    //Q.4 implement the getTotalPurchuse method which returns the total purhucse for all songs in the cart
    return 0;
}

}

还应将Song作为单独的文件移出

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

答案 1 :(得分:-1)

这应该是您的问题的答案

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