Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按id java搜索arraylist_Java_Arraylist - Fatal编程技术网

按id java搜索arraylist

按id java搜索arraylist,java,arraylist,Java,Arraylist,下面的程序要求给出玩家的数量和姓名。我想把名字放在一个arraylist中,并通过id返回它们 private static ArrayList<Player>Playerlist=new ArrayList<Player>(); public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s; { Syst

下面的程序要求给出玩家的数量和姓名。我想把名字放在一个arraylist中,并通过id返回它们

private static ArrayList<Player>Playerlist=new ArrayList<Player>();

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);
    String s;


   {
        System.out.printf("number of players(2 -4)? ");

    int p = scanner.nextInt();
    scanner.nextLine();
    for(int i=0;p>4 || p<2;i++)
     {

         System.out.printf("only 2-4 players.\n");
         System.out.printf("number of players(2 -4)?");
         p = scanner.nextInt();
         scanner.nextLine();

     }
   Player pl=new Player();
   int m=1;
    for(int k=1;k<=p;k++)
     {


         System.out.printf("give the name of the player %d: ",k);
         s= scanner.nextLine();
         pl.setName(s,m);
         System.out.printf(pl.getName());
         Playerlist.add(pl);
       m++;
      }
  public class Player {

  private String name;
  private int id;

  public Player () {
   }

  Player(String val,int k) {
    this.name = val;
    this.id=k;}
  /**
   * getName
  *
  */
public String getName () {
    return name;
}

/**
 * setName

 */
public void setName (String val,int k) {
    this.name = val;
    this.id=k;
}
public void displayStudentDetails(){
    System.out.println("ID is "+id);
    System.out.println("Name is "+name)};
private static ArrayListPlayerlist=new ArrayList();
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
字符串s;
{
System.out.printf(“玩家数量(2-4)”;
int p=scanner.nextInt();
scanner.nextLine();

对于(int i=0;p>4 | | p而言,更好的解决方案是使用
HashMap
,id为键,名称为值。然后您可以轻松搜索:

HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "Robert");
map.put(2, "Jeff");
编辑:好的,如果您需要的数据不仅仅是名称,比如分数,那么您需要创建一个数据类型。比如:

public class Player {
    private String name;
    private int score;
    // etc.

    public Player(String name, int score) {
        this.name = name;
        this.score = score;
    }
}
然后,您可以将此类对象添加到地图中,如:

map.put(1, new Player("Robert", 10));

忽略这样一个事实,即此时在不同的文件中应该有没有全局变量的类

/**
 * Searches the PlayerList Arraylist for a Player having the ID of the parameter passed
 */
public Player searchByID(int id) {
  //Do some sort of check on id to ensure its valid?
  for (int i = 0; i < PlayerList.size(); i++) {
    if (PlayerList.get(i).getID() == id) {
      return PlayerList.get(i);
    }
  }

  return null;
}
/**
*在PlayerList Arraylist中搜索传递了参数ID的玩家
*/
公共播放器searchByID(int-id){
//对id进行某种检查以确保其有效?
对于(int i=0;i
ArrayList可能是错误的数据结构。如果您想按id顺序检索元素,那么如果id都是规则的,您就需要一个HashMap,因为您可以确定“id是1到10,没有未使用的id”。如果id集是稀疏的,则您需要根据其他用例使用TreeMap或TreeSet。

如果您需要按id获取播放机的情况完全基于代码添加播放机的方式,则您可以执行以下操作:

public Player getPlayerById(int id)
{
  return PlayerList.get(id - 1);
}
由于您正在为第一个玩家线性地从1开始分配玩家ID。如果您正在修改或以其他方式更改玩家和ID的顺序,则需要使用其他建议之一(但如果您不这样做,则这是最优化的解决方案)


注意:上面的代码将引发ArrayIndexOutOfBoundsException,如果该id在播放器列表中不存在,您是否希望根据处理方式修改代码。

我不理解“按id搜索”。您可以将玩家的对象添加到ArrayList中,这样您就可以通过查看数组列表来获取每个玩家的id和名称。能否举例说明您想要什么?根据您分配id的方式,第一个玩家的id从1开始,然后增加1(因此玩家2的id为2)。如果总是这样,那么要让玩家获得一个“id”,您只需执行Playerlist.get(id-1);@des:如果解决了您的问题,请不要忘记选择“接受答案”。)您知道是否可以在我的HashMap中添加更多内容…例如,我想将分数添加到esch player,如下-->1,Robert,300或2,Jeff,450和i如果我按id搜索,它将返回名称和分数…非常感谢您的回复…我按照您所说的创建了新的类播放器,当我在main中编写此内容时“map.put(1,新玩家(“Robert”,10));“我收到消息”找不到符号:method put(int,game.Player)”…@des,你必须将地图声明更改为HashMap。是的,我找到了!还有一个问题(最后一个我保证!)我搜索“Player map1=map.get(2);System.out.print(map1);“为了返回id=2的玩家的姓名和分数,它将返回此游戏。Player@60aeb03...do你知道为什么吗?
public Player getPlayerById(int id)
{
  return PlayerList.get(id - 1);
}