为什么我能';不要用java打印列表?
我有一个java程序,在主类中有一个字符串类型的列表为什么我能';不要用java打印列表?,java,arrays,list,printing,Java,Arrays,List,Printing,我有一个java程序,在主类中有一个字符串类型的列表 public static List<String> Player_cards = new ArrayList<String>(); 我尝试使用get打印列表中的值: System.out.println(Player_cards.get(0)); 当我尝试打印时,会出现以下错误: Exception in thread "main" java.lang.Out0fMemoryError: Java heap sp
public static List<String> Player_cards = new ArrayList<String>();
我尝试使用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);
}
}
import java.util.*;
公共班机
{
公共静态字符串person;//凶手
公共静态字符串位置\;//犯罪发生的位置
公共静态字符串武器\;//用于谋杀的武器
公共静态字符串[]Persons={“Scarlet先生”,
“普卢姆教授”,
“孔雀”,
“芥末上校”,
“格林先生”,
“兰花博士”};
公共静态字符串[]位置={“大厅”,
“钢琴曲”,
“温室”,
“阅览室”,
“游泳池”,
“卧室”,
“餐厅”,
“图书馆”,
“基兴”};
公共静态字符串[]武器={“刀”,
“铲子”,
“刀锋”,
“扳手”,
“绳子”,
“酒吧”};
公共静态字符串[]All={“刀”,
“铲子”,
“刀锋”,
“扳手”,
“绳子”,
“酒吧”,
“大厅”,
“钢琴曲”,
“温室”,
“阅览室”,
“游泳池”,
“卧室”,
“餐厅”,
“图书馆”,
“基钦”,
“斯佳丽先生”,
“普卢姆教授”,
“孔雀”,
“芥末上校”,
“格林先生”,
“兰花博士”};
//公共静态字符串[]玩家卡=新字符串[3];
//列出玩家卡=新建ArrayList();
public static List Player_cards=new ArrayList();
公共静态整数移动(整数位置){
//此函数用于输入位置并返回基于
//游戏规则。
而(1==1){
随机r=新随机();
int dice_1=1+r.nextInt(6);
int dice_2=1+r.nextInt(6);
如果((骰子1+骰子2)%2==0){
int[]选项={2,4,6,8};
int rand=r.nextInt(4);
如果(选项[rand]!=位置和选项[rand]!=位置-1和选项[rand]!=位置+1){
System.out.printf(“骰子一:”);
System.out.println(dice_1);
System.out.printf(“骰子二:”);
System.out.println(骰子2);
返回选择[rand];
}
}否则{
int[]选项={1,3,5,7,9};
int rand=r.nextInt(5);
如果(选项[rand]!=位置和选项[rand]!=位置-1和选项[rand]!=位置+1){
System.out.printf(“骰子一:”);
System.out.println(dice_1);
System.out.printf(“骰子二:”);
System.out.println(骰子2);
返回选择[rand];
}
}
}
}
公共静态void start(){
//此功能将启动游戏。
//随机选择1件武器、1个人和1个地点作为游戏的解决方案。
随机r=新随机();
内部和外部人员=r.nextInt(5);
int rand_place=r.nextInt(8);
int rand_武器=r.nextInt(5);
人员=人员[随机人员];
地点=地点[随机地点];
武器=武器[随机武器];
while(true){
int rand_1=r.nextInt(21);
如果(全部[rand_1]!=个人和全部[rand_1]!=地点和全部[rand_1]!=武器){
玩家卡。添加(所有[rand_1]);
}
int rand_2=r.nextInt(21);
如果(全部[rand_2]!=个人和全部[rand_2]!=地点和全部[rand_2]!=武器){
玩家卡。添加(所有[rand_2]);
}
int rand_3=r.nextInt(21);
如果(所有[rand_3]!=个人和所有[rand_3]!=地点和所有[rand_3]!=武器){
玩家卡。添加(所有[rand_3]);
}
}
}
公共静态void main(字符串[]args){
系统输出打印LN(移动(3));
start();
//System.out.println(Player_cards.toString());
系统输出打印LN(玩家卡);
}
}
您有一个无限循环
while(1==1)
这会占用你的全部记忆。
可能您忘记在循环中设置中断。在我的代码中,我添加了3个元素,而不是更多!你能出示所有相关的代码吗?可能是整个
Main
类。我试图从另一个字符串类型的数组中添加元素。这重要吗<代码>玩家卡。添加(所有[rand])这里我想将All
数组中的随机项添加到我的列表中。@AmirrezaRiahi查看异常,没有调用func
方法。您能否显示所有相关代码,包括main
和start
方法?您正在无限循环中添加项。c语言中的内存量
while(1==1)