Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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
Java 如何在不同类型的堆栈中存储二维数组?_Java_Arrays - Fatal编程技术网

Java 如何在不同类型的堆栈中存储二维数组?

Java 如何在不同类型的堆栈中存储二维数组?,java,arrays,Java,Arrays,我正在自学Java,在YouTube上遇到了一些练习问题。我已经设法进入了堆栈章节,现在我被卡住了。我已经创建了一个棒球选秀类,现在我需要一个类来存储球员信息。是否可以将数组存储在包含两种不同类型的堆栈中?在每个实例上推送字符串int的堆栈 “山姆”,24岁 “雷诺兹”,30岁 “沃克”,41岁 我需要将上面的信息放在堆栈中。建议?请不要只发布答案代码,这不会有帮助正如李宗正所建议的,您可以创建一个包含2个成员变量的新类 public class Player{ String name

我正在自学Java,在YouTube上遇到了一些练习问题。我已经设法进入了堆栈章节,现在我被卡住了。我已经创建了一个棒球选秀类,现在我需要一个类来存储球员信息。是否可以将数组存储在包含两种不同类型的堆栈中?在每个实例上推送字符串int的堆栈

“山姆”,24岁 “雷诺兹”,30岁 “沃克”,41岁


我需要将上面的信息放在堆栈中。建议?请不要只发布答案代码,这不会有帮助

正如李宗正所建议的,您可以创建一个包含2个成员变量的新类

public class Player{

    String name;
    int age; //I'm not sure what the integer is supposed to represent

    //constructor
    Player(String playerName, int playerAge){
        this.name = playerName;
        this.age = playerAge;
    }

    //getters
    public String getName(){
        return this.name;    
    }

    public int getAge(){
        return this.age;    
    }

}
然后,您可以使用new关键字创建具有所需值的播放器对象

Player sam = new Player("Sam", 24);
然后将玩家添加到堆栈中

stack.push(sam);

正如Zong Zheng Li所建议的,您可以创建一个包含2个成员变量的新类

public class Player{

    String name;
    int age; //I'm not sure what the integer is supposed to represent

    //constructor
    Player(String playerName, int playerAge){
        this.name = playerName;
        this.age = playerAge;
    }

    //getters
    public String getName(){
        return this.name;    
    }

    public int getAge(){
        return this.age;    
    }

}
然后,您可以使用new关键字创建具有所需值的播放器对象

Player sam = new Player("Sam", 24);
然后将玩家添加到堆栈中

stack.push(sam);

如果您只想存储像[“Sam”、24“Reynolds”、30“Walker”、41]这样的数组,可以将数组创建为对象。通过这种方式,您可以存储任何类型的数据。另外,当您想要获取堆栈之类的数据时,可以从数组末尾开始读取数据。

如果您只想存储像[“Sam”、24“Reynolds”、30“Walker”、41]这样的数组,可以将数组创建为对象。通过这种方式,您可以存储任何类型的数据。此外,当您想要获取堆栈之类的数据时,可以开始读取数组末尾的数据。

为什么不创建一个对象来封装信息呢?只需创建一个以string和int作为数据成员的类。并将该对象推入堆栈。类似这样的类playerinformation{String name;int age}并创建playerinformation的对象并将其推入堆栈为什么不创建一个对象来封装信息呢?只需创建一个以String和int作为数据成员的类。并将该对象推入堆栈。类似于这个类的playerinformation{String name;int age}并创建playerinformation对象并将其推入堆栈