【发布时间】:2020-05-20 08:38:44
【问题描述】:
我有一个 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);
}
}
【问题讨论】:
-
在我的代码中,我添加了 3 个元素,而不是更多!
-
你能显示所有相关代码吗?也许整个
Main类。 -
我试图从另一个数组中添加字符串类型的元素。重要吗?
Player_cards.add(All[rand]);在这里我想将All数组中的随机项添加到我的列表中。 -
@AmirrezaRiahi 查看异常,
func方法未被调用。你能显示所有相关代码,包括你的main和start方法吗? -
您正在无限循环中添加项目。您计算机中的内存量不是无限的。