arraylist问题

vpfxa7rd  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(319)

我想知道如何引用arraylist一个不同于在中声明的方法。例如,我正在编写一个程序,允许我创建一个歌曲播放列表,所以在一个方法中我有createaplaylist,然后在另一个方法中我有shuffle()。在我的playlist方法中,我创建了一个arraylist,但是在我的shuffle方法中使用这个arraylist时遇到了问题。下面有一些上下文代码:

public static void createAPlaylist() {
        try {
            System.out.println("We have the following tracks:");
            ArrayList<String> songs = new ArrayList<String>();
            String firstSong = jukebox.allTracks.get(0).getTitle();
            songs.add(firstSong);

            for (int index = 0; index < count; index++) {
                System.out.println(SPACES + (index + 1) + ". " + jukebox.allTracks.get(index).getTitle());
            }

            System.out.println("Select a track to add to the playlist: ");
            int songNumber = input.nextInt();
            String songSelected = songs.get(songNumber);
            songs.add(songSelected);
            input.nextLine();

        } catch (Exception e) {
            System.out.println("\nplease select a valid song number.");
        }
    }
ryevplcw

ryevplcw1#

在java中,变量只在创建它们的上下文中可用—因此如果您创建 ArrayList 在方法内部,除非将其存储在方法的类中,或者从生成它的方法返回变量,否则不能在该方法外部访问它。
你可以 ArrayList 在方法之外,作为类字段,如下所示:

public class Foo {

    private static ArrayList<String> arrayList = new ArrayList<String>();

    public static void createAPlaylist() {
        arrayList.add();

        etc...
    }
}

或者你可以把钱还给我 ArrayList 按照这样的方法:

public class Foo {
    public static void main(String[] args) {
        ArrayList<String> arrayList = createAPlaylist();
    }

    public static ArrayList<String> createAPlaylist() {

    ArrayList<String> songs = new ArrayList<String>();

    // Your code here

    // Note that you have to return the list from 
    // inside the catch block!

    // I’d recommend creating the ‘songs’ ArrayList 
    // outside of the ‘try’ block, so that you can 
    // have a fallback if something fails in the ‘try’

    return songs;
    }
}

我不知道你是不是想让这一切都静下来。我认为它会更好地作为非静态的,但这是另一个问题的问题,所以我把它留在示例中。
抱歉,如果这不是完美的格式-我在一个移动设备上,没有我的ide。

4nkexdtk

4nkexdtk2#

这就是方法参数的用途:

public static void createAPlaylist() {
    ArrayList<String> songs = new ArrayList<>();
    shuffle(songs);
}

public static void shuffle(ArrayList<String> songs) {
    // Do stuff with your ArrayList here
}
azpvetkf

azpvetkf3#

您可以从 createAPlaylist 方法并将其传递给 shuffle 方法:
比如:

public static List<String> createAPlaylist() {
  ...
  ...
  ... 
  return songs;
 }

/// and then in shuffle method receive that as parameter :

public static void shuffle(List<String> songs){
    // access that songs list by
}

或者你可以:
将arraylist声明为类变量而不是方法变量。。
比如:

public class ClassName{
 public static ArrayList<String> songs = new ArrayList<String>();

 public static void createAPlaylist() {
  ...
  ...
  ... 
  // reset the songs.
  songs = new ArrayList<String>();
  ...
 }

/// and then in another method:

public static void suffle(){
    // access that songs list by
   List<String> createdSongs = ClassName.songs;
}

相关问题