java - 为什么我不能在java中打印列表?
问题描述
我有一个 java 程序,在主类中我有一个字符串类型的列表
public static List<String> Player_cards = new ArrayList<String>();
然后我尝试从一个函数将项目添加到这个列表中。
public static void func(){
Player_cards.add("Hello");
}
我尝试使用以下方法打印列表中的值get
:
System.out.println(Player_cards.get(0));
当我尝试打印错误时:
Exception in thread "main" java.lang.Out0fMemoryError: Java heap space at
java.util.Arrays.copy0f(Arrays.java:3210) at java.util.Arrays.copy0f(Arrays.java:3181) at
java.util.ArrayList.grow(ArrayList.java:265) at
java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239) at
java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231) at
java.util.ArrayList.add(ArrayList.java:462)
at Main.start(Main.java:116)
at Main.main(Main.java:142)
我还尝试打印完整列表:
System.out.println(Player_cards);
但结果相同。这是我的整个代码:
import java.util.*;
public class Main
{
public static String person_; //the murderer
public static String place_; //the place where crime committed
public static String weapon_; //the weapon used in murder
public static String[] Persons = {"Mr.Scarlet",
"Prof.Plum",
"Peacock",
"Colonel Mustard",
"Mr.Green",
"Dr.Orchid"};
public static String[] Places = {"Hall",
"PianoRoom",
"GreenHouse",
"ReadingRoom",
"Pool",
"BedRoom",
"DiningRoom",
"Library",
"Kichen"};
public static String[] Weapons = {"knife",
"shovel",
"blade",
"wrench",
"rope",
"bar"};
public static String[] All = {"knife",
"shovel",
"blade",
"wrench",
"rope",
"bar",
"Hall",
"PianoRoom",
"GreenHouse",
"ReadingRoom",
"Pool",
"BedRoom",
"DiningRoom",
"Library",
"Kichen",
"Mr.Scarlet",
"Prof.Plum",
"Peacock",
"Colonel Mustard",
"Mr.Green",
"Dr.Orchid"};
// public static String[] Player_cards = new String[3];
// List Player_cards = new ArrayList();
public static List<String> Player_cards = new ArrayList<String>();
public static int move(int position){
// this function input the position and return a random position based on
// rules of game.
while(1==1){
Random r = new Random();
int dice_1 = 1+r.nextInt(6);
int dice_2 = 1+r.nextInt(6);
if((dice_1+dice_2)%2==0){
int[] choices = {2,4,6,8};
int rand = r.nextInt(4);
if(choices[rand]!=position && choices[rand]!=position-1 && choices[rand]!= position+1){
System.out.printf("dice one:");
System.out.println(dice_1);
System.out.printf("dice two:");
System.out.println(dice_2);
return choices[rand];
}
}else{
int[] choices = {1,3,5,7,9};
int rand = r.nextInt(5);
if(choices[rand]!=position && choices[rand]!=position-1 && choices[rand]!= position+1){
System.out.printf("dice one:");
System.out.println(dice_1);
System.out.printf("dice two:");
System.out.println(dice_2);
return choices[rand];
}
}
}
}
public static void start(){
//this function will start the game.
//randomly choose 1 weapon, 1 person and 1 place as the solution of the game.
Random r = new Random();
int rand_person = r.nextInt(5);
int rand_place = r.nextInt(8);
int rand_weapon = r.nextInt(5);
person_ = Persons[rand_person];
place_ = Places[rand_place];
weapon_ = Weapons[rand_weapon];
while(true){
int rand_1 = r.nextInt(21);
if (All[rand_1] != person_ && All[rand_1] != place_ && All[rand_1] != weapon_){
Player_cards.add(All[rand_1]);
}
int rand_2 = r.nextInt(21);
if (All[rand_2] != person_ && All[rand_2] != place_ && All[rand_2] != weapon_){
Player_cards.add(All[rand_2]);
}
int rand_3 = r.nextInt(21);
if (All[rand_3] != person_ && All[rand_3] != place_ && All[rand_3] != weapon_){
Player_cards.add(All[rand_3]);
}
}
}
public static void main(String[] args) {
System.out.println(move(3));
start();
// System.out.println(Player_cards.toString());
System.out.println(Player_cards);
}
}
解决方案
你有一个无限循环
while(1==1)
这将占用你的全部记忆。可能您忘记在循环中设置中断。
推荐阅读
- python - 无法使用请求连接到 Tor,而我使用 selenium 也是如此
- firebase - firebase 实时数据库:TypeError:无法将未定义或 null 转换为对象
- javascript - 如何在 javascript 中将 jQuery 的值分配给 PHP SESSION
- c# - 为什么要创建父类的类型来存储子类的对象?
- python - 我们可以将 fastText 与 SVM 一起集成吗?
- javascript - 在nodejs中通过带有构造函数的模块公开多个对象
- go - 如何在 Go 语言的模板中获取嵌入变量
- java - 本机库 D:\jdk 1.8.0 181\jre1.8.0_181\bin\glass.dll 已在另一个类加载器中加载
- actions-on-google - 启用智能显示器
- docker - 无法从 Jenkins 管道连接远程 docker 主机 api