简体   繁体   English

传递清单 <String> 作为参数

[英]passing List<String> as a parameter

I have a function 我有一个功能

public class PlayerManager {
    public PlayerManager(List<String> players){
    //something to manipulate the player list..
    }
    private static class PlayerNode {
      public String name;    
      public PlayerNode next;
      //...
    }
}

What I need is to create PlayerNode(it works like a LinkedList) in the constructor based on the data in the player list. 我需要的是根据播放器列表中的数据在构造函数中创建PlayerNode(它的工作方式类似于LinkedList)。 What comes to my mind is to create an array and then iterate it and create the PlayerNode List: 我想到的是创建一个数组,然后对其进行迭代并创建PlayerNode List:

String[] playerArray = players.toArray(new String[players.size()]);

But the assignment requirement says I can't construct any arrays, ArrayLists, LinkedLists, Stacks, Queues, or other data structures. 但是分配要求说我不能构造任何数组,ArrayList,LinkedList,堆栈,队列或其他数据结构。

So my question is if I can not create any class implemented List interface. 所以我的问题是我是否不能创建任何实现类的List接口。 How can I handle the data in it? 如何处理其中的数据?

Thanks 谢谢

From the sounds of it, you are going to be passing some sort of list into your method, so you could just do something like this: 从它的声音来看,您将要在您的方法中传递某种列表,因此您可以执行以下操作:

public class PlayerManager {
    public PlayerManager(List<String> players){
         for(String player : players){
             //do something with your player
         }
    }
}

This will allow you to do something with the list of players without knowing what data structure you are going to be using. 这将使您可以对播放器列表进行操作,而无需知道您将要使用的数据结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM