我有一个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);
}
}
答案 0 :(得分:2)
您有无限循环
while(1==1)
,这将占用您的全部内存。 可能您忘了在循环中设置中断。