Java 为什么不';t如果语句与布尔数组列表一起工作?
我在这里试图做的是总是从我的字符串arraylist中获取一个随机值,为此我使用另一个arraylist,它包含与字符串相同数量的布尔值 当我需要一个随机字符串时,我会这样做:Java 为什么不';t如果语句与布尔数组列表一起工作?,java,arrays,arraylist,Java,Arrays,Arraylist,我在这里试图做的是总是从我的字符串arraylist中获取一个随机值,为此我使用另一个arraylist,它包含与字符串相同数量的布尔值 当我需要一个随机字符串时,我会这样做: randomstring = r.nextInt(100); String mystring = c.getAnswear(randomstring); if(c.getAnswear(randomstring) == null){ randomstring = r.nextInt(100); myst
randomstring = r.nextInt(100);
String mystring = c.getAnswear(randomstring);
if(c.getAnswear(randomstring) == null){
randomstring = r.nextInt(100);
mystring = c.getAnswear(randomstring);
System.out.println("Random again!!!!!!!!!!!!!!!");
}
这是c.getAnswear内部的外观
List<String> strings = new ArrayList<String>();
strings.add("blablabla");
//And another 100 of theese
//Then we have theese boolean array
ArrayList<Boolean> trueornot = new ArrayList<Boolean>();
trueornot.add(false);
//Another 100 of them
//Then we have this
String Answear = null;
if(trueornot.get(indexNum).equals(true)){
Answear = null;
System.out.println(Answear);
System.out.println("Already used string random again!");
}
else if(trueornot.get(indexNum).equals(false)){
trueornot.add(indexNum, true);
Answear = strings.get(indexNum);
System.out.println(Answear);
System.out.println("Something in the array was set to true");
}
return Answear;
List strings=new ArrayList();
字符串。添加(“blablabla”);
//还有另外100个
//然后我们得到了这个布尔数组
ArrayList trueornot=新的ArrayList();
trueornot.add(false);
//还有100个
//那么我们有这个
字符串Answear=null;
if(trueornot.get(indexNum).equals(true)){
Answear=null;
系统输出打印(Answear);
System.out.println(“已再次使用随机字符串!”);
}
else if(trueornot.get(indexNum).equals(false)){
添加(indexNum,true);
Answear=strings.get(indexNum);
系统输出打印(Answear);
System.out.println(“数组中的某些内容被设置为true”);
}
退货;
但是当我尝试这样做的时候,我可以随机化我的字符串,我想要多少,它会打印到控制台上,一些东西被设置为true,但是我看到很多次相同的字符串被再次使用,System.out.println(“已经使用过的字符串再次随机!”)代码>从不在控制台中打印
当我调用这个getAnswear时,我有if语句(c.getAnswear(randomstring)==null)
里面的行System.out.println(“再次随机!!!!!!!”)代码>永远不会进入控制台
我该怎么做?似乎getAnswear
中的答案字符串从不为null,这很奇怪,因为我正在将布尔值设置为true。trueornot.add(indexNum,true)
这会将该值添加到列表中,并将该索引处的上一个值向下移动到列表中
您应该使用trueornot.set(…)
替换值
更新:
这是c.getAnswear内部的外观
List<String> strings = new ArrayList<String>();
strings.add("blablabla");
//And another 100 of theese
//Then we have theese boolean array
ArrayList<Boolean> trueornot = new ArrayList<Boolean>();
trueornot.add(false);
//Another 100 of them
//Then we have this
String Answear = null;
if(trueornot.get(indexNum).equals(true)){
Answear = null;
System.out.println(Answear);
System.out.println("Already used string random again!");
}
else if(trueornot.get(indexNum).equals(false)){
trueornot.add(indexNum, true);
Answear = strings.get(indexNum);
System.out.println(Answear);
System.out.println("Something in the array was set to true");
}
return Answear;
List strings=new ArrayList();
字符串。添加(“blablabla”);
//还有另外100个
//然后我们得到了这个布尔数组
ArrayList trueornot=新的ArrayList();
trueornot.add(false);
//还有100个
//那么我们有这个
如果每次调用getAnswer时都会发生这种情况,那么每次都会重新创建字符串和布尔列表,以前的所有更改都会丢失。
字符串和trueornot
应该是类字段,而不是局部变量。trueornot.add(indexNum,true)
这会将该值添加到列表中,并将该索引处的上一个值向下移动到列表中
您应该使用trueornot.set(…)
替换值
更新:
这是c.getAnswear内部的外观
List<String> strings = new ArrayList<String>();
strings.add("blablabla");
//And another 100 of theese
//Then we have theese boolean array
ArrayList<Boolean> trueornot = new ArrayList<Boolean>();
trueornot.add(false);
//Another 100 of them
//Then we have this
String Answear = null;
if(trueornot.get(indexNum).equals(true)){
Answear = null;
System.out.println(Answear);
System.out.println("Already used string random again!");
}
else if(trueornot.get(indexNum).equals(false)){
trueornot.add(indexNum, true);
Answear = strings.get(indexNum);
System.out.println(Answear);
System.out.println("Something in the array was set to true");
}
return Answear;
List strings=new ArrayList();
字符串。添加(“blablabla”);
//还有另外100个
//然后我们得到了这个布尔数组
ArrayList trueornot=新的ArrayList();
trueornot.add(false);
//还有100个
//那么我们有这个
如果每次调用getAnswer时都会发生这种情况,那么每次都会重新创建字符串和布尔列表,以前的所有更改都会丢失。
strings
和trueornot
应该是类字段,而不是局部变量。可能是这样,您想将值设置为true,而不是插入它吗
您可以这样做:
trueornot.add(indexNum, true);
但要设置值,必须执行以下操作:
trueornot.set(indexNum, true);
也许,您想将值设置为true,而不是插入它
您可以这样做:
trueornot.add(indexNum, true);
但要设置值,必须执行以下操作:
trueornot.set(indexNum, true);
我以前确实使用过trueornot.set(),但它发生的事情与没有发生的事情是一样的。。我只是想试一下。阿杜。。这是真的,但我不能将其置于此方法之外,也不能在另一个方法内创建它。如果要在后续调用中保留任何更改,则必须执行此操作。我不能在类字段中初始化Arraylist格式Classic构造函数中的初始化。我确实使用了trueornot.set()以前发生过,但现在发生了同样的事情。。我只是想试一下。阿杜。。这是真的,但我不能将它放在这个方法之外,也不能在另一个方法内创建它。如果希望在后续调用中保留任何更改,则必须这样做。我不能在类字段中初始化Arraylist在类的构造函数中格式化初始化,正如我在另一个Answare上评论说我使用了trueornot.set()以前发生过,但现在发生了同样的事情。。我只是尝试一下。addI没有看到这个答案,当我发布我的^^和我在另一个answear上的评论一样说这个时,我以前确实使用过trueornot.set(),但它发生的事情和它没有发生的事情是一样的。。我只是想试一下。阿迪没有看到那个答案,当我发布我的答案时^^