简体   繁体   English

如果语句不能与我的布尔数组列表一起使用,为什么不呢?

[英]Why don't if statements work with my boolean arraylist?

What I'm trying to do here is to always get a random value from my String arraylist and to do this I'm using another arraylist containing the same amount of booleans as the strings. 我在这里试图做的是始终从我的String数组列表中获取一个随机值,并且为此,我正在使用另一个包含与字符串相同数量的布尔值的arraylist。

When I need a random string I do this: 当我需要随机字符串时,请执行以下操作:

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!!!!!!!!!!!!!!!");
}

And this is how it looks inside c.getAnswear 这就是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;

But when I try to do this can randomize my string how much I want and it prints to console something was set to true but I see many times the same string gets used again and the System.out.println("Already used string random again!"); 但是,当我尝试执行此操作时,可以将我的字符串随机分配给我,然后打印以将某些内容设置为true,以便控制台输出,但是我看到很多次相同的字符串被再次使用,并且System.out.println("Already used string random again!"); never gets printed in the console 永远不会在控制台中打印

Same with when I call this getAnswear I have the if statement (c.getAnswear(randomstring) == null) the line inside there System.out.println("random again!!!!!!!"); 与调用此getAnswear时相同,我有if语句(c.getAnswear(randomstring) == null)里面的行System.out.println("random again!!!!!!!"); never gets into the console. 永远不会进入控制台。

How would I make it work? 我将如何运作? Seems like the answer string inside getAnswear is never null and that's strange because I'm setting the booleans to true. 似乎getAnswear的答案字符串永远不会为null,这很奇怪,因为我将布尔值设置为true。

trueornot.add(indexNum, true)
this adds the value to the list and shifts the previous value at that index down the list. 这会将值添加到列表中,并将该索引处的前一个值向下移动到列表中。

you should use trueornot.set(...) which replaces the value... 您应该使用trueornot.set(...)来替换值...

update: 更新:

And this is how it looks inside c.getAnswear 这就是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

if this actually happens every time getAnswer is called then you're recreating the strings and the boolean list everytime, all previous changes are lost. 如果在每次调用getAnswer时实际上发生这种情况,那么您每次都在重新创建字符串和布尔值列表时,所有以前的更改都将丢失。
strings and trueornot should be class fields and not local variables. stringstrueornot应该是类字段,而不是局部变量。

may it be, you want to set your value true, instead of insert it? 可能是,您想要将值设置为true而不是将其插入吗?

you do this: 你做这个:

trueornot.add(indexNum, true);

but to set a value you have to do this: 但是要设置一个值,您必须这样做:

trueornot.set(indexNum, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM