Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 分支数组-如果包含;“真的”;_Java_For Loop - Fatal编程技术网

Java 分支数组-如果包含;“真的”;

Java 分支数组-如果包含;“真的”;,java,for-loop,Java,For Loop,我有一个for循环,它在数组中循环 for(int i = 0; i<=(clientListpost.length -1); i++){ if(clientpost[i] == true) { if((clientpost[i+1] == true) || (clientpost[i+2] == true) || (clientpost[i+3] == true)) { clientString[i] = client[i] + ",";

我有一个for循环,它在数组中循环

for(int i = 0; i<=(clientListpost.length -1); i++){
    if(clientpost[i] == true) {
      if((clientpost[i+1] == true) || (clientpost[i+2] == true) || (clientpost[i+3] == true)) {
        clientString[i] = client[i] + ",";
      } else {
        clientString[i] = client[i];
      }
    } 
  }
输出:

如果仅选择了1015=>1015

如果选择1030和1045=>10301045

如果选择了1045和1070=>10451070


提前谢谢

看起来您可以创建一个附加方法来确定是否需要向
clientString[i]

for(int i = 0; i<=(clientListpost.length -1); i++){
    if(clientpost[i]) {
        clientString[i] = client[i];

        if (isClientPostEnabled(clientListpost,clientListpost.length -1, i)) {
            clientString[i] += ",";
        }
    }
}

public static boolean isClientPostEnabled(List<bool> clientListpost,int index, int startIndex) {
    for (int i = startIndex; i < index - startIndex; i++) {
        if (clientpost[i]) {
            return true;
        }
    }
    return false;
}

for(int i=0;i看起来您可以创建一个额外的方法来确定是否需要将逗号添加到
clientString[i]

for(int i = 0; i<=(clientListpost.length -1); i++){
    if(clientpost[i]) {
        clientString[i] = client[i];

        if (isClientPostEnabled(clientListpost,clientListpost.length -1, i)) {
            clientString[i] += ",";
        }
    }
}

public static boolean isClientPostEnabled(List<bool> clientListpost,int index, int startIndex) {
    for (int i = startIndex; i < index - startIndex; i++) {
        if (clientpost[i]) {
            return true;
        }
    }
    return false;
}

对于(int i=0;i我将在这里做一些假设(也请参考您问题评论中我的问题):

  • 您确实需要像这样构建阵列
  • 您已确保未抛出ArrayIndexOutOfBoundsException
与其检查是否添加了任何后续元素,不如向后迭代:

boolean subsequentElementsSelected = false;
for(int i = clientListpost.length -1; i >= 0; i--){
  if(clientpost[i]) { //or clientpost[i] == true
    if(subsequentElementsSelected ) { 
      clientString[i] = client[i] + ",";
    } else {
      //only true for the last element to be added/set
      clientString[i] = client[i];
    }

    //once we've added an element at the end this will stay true
    subsequentElementsSelected  = true;
  } 
}
根据您的评论:

最后我把它们简化成一个字符串

如果不需要
clientString
数组,只需在字符串后追加,并在需要时添加逗号:

StringBuilder builder = new StringBuilder();
for(int i = 0; i <= clientListpost.length -1; i++){
  if(clientpost[i]) { //or clientpost[i] == true

    //if there's already something in the String, add a comma first
    if(builder.length() > 0) { 
      builder.append(",");
    } 

    //add the element
    builder.append(client[i]);
  } 
}
clientUrl += builder.toString(); //assumes there's more in clientUrl, otherwise just assign
StringBuilder=新建StringBuilder();

对于(int i=0;i我将在这里做一些假设(请同时参考您问题评论中我的问题):

  • 您确实需要像这样构建阵列
  • 您已确保未抛出ArrayIndexOutOfBoundsException
与其检查是否添加了任何后续元素,不如向后迭代:

boolean subsequentElementsSelected = false;
for(int i = clientListpost.length -1; i >= 0; i--){
  if(clientpost[i]) { //or clientpost[i] == true
    if(subsequentElementsSelected ) { 
      clientString[i] = client[i] + ",";
    } else {
      //only true for the last element to be added/set
      clientString[i] = client[i];
    }

    //once we've added an element at the end this will stay true
    subsequentElementsSelected  = true;
  } 
}
根据您的评论:

最后我把它们简化成一个字符串

如果不需要
clientString
数组,只需在字符串后追加,并在需要时添加逗号:

StringBuilder builder = new StringBuilder();
for(int i = 0; i <= clientListpost.length -1; i++){
  if(clientpost[i]) { //or clientpost[i] == true

    //if there's already something in the String, add a comma first
    if(builder.length() > 0) { 
      builder.append(",");
    } 

    //add the element
    builder.append(client[i]);
  } 
}
clientUrl += builder.toString(); //assumes there's more in clientUrl, otherwise just assign
StringBuilder=新建StringBuilder();


对于(int i=0;i)迭代数组的最终目标是什么?等等,我写了一个例子。我怀疑您有4个索引完全相同的数组。也许这些应该是对象?(clientpost[i+1])| |(clientpost[i+2])| |(clientpost[i+3]))-这?你怎么没有得到AIOOBE?你迭代数组的最终目标是什么?等等,我写了一个例子。你有4个完全相同索引的不同数组,这一事实对我来说很可疑。也许这些应该是对象?(clientpost[I+1])| |(clientpost[I+2])| |(clientpost[I+3]))-这?你怎么没有得到AIOOBE?那不是它。我想你的答案总是在值的末尾放一个逗号,如果它被选中。但是我只需要在当前数组后面的数组是真的或者我错了的情况下,在值的末尾放一个逗号?只有当
isClientPostEnabled
返回true时,它才会放逗号。方法
isClientPostEnabled
检查是否至少有一个
true
值从
startIndex
开始。如果所有剩余值都是
false
,则
isClientPostEnabled
也会返回false。逗号不会放在ClientString噢,是的,我现在会尝试!不是这样。我想你的答案总是在t的末尾放一个逗号如果选择了,则输入值。但是如果当前数组后面的数组为真或我错了,则我需要它仅在末尾加逗号。只有当
isClientPostEnabled
返回真时,它才会加逗号。方法
isClientPostEnabled
检查是否至少有一个
true
值从
startIndex
开始。如果所有值都是真的maining值是
false
而不是
isClientPostEnabled
也返回false。逗号将不会被放入ClientString噢,是的,我现在就试试看!对于第一个问题:我本可以这么做,但我不想让它变得复杂,因为它已经是^^^。对于第二个问题:不,只有逗号。但是你的答案看起来也一样很好,我会尝试第二个,我会让你知道。第二个StringBuilder示例也很有效。它实际上比另一个答案更好,因为它较短。但我在Flatter中工作,因此StringBuilder不可用。因此我需要对其进行一点更改。我还有一个问题:为什么它不在最后一个客户端添加逗号?@Timebreaker900我拒绝了您的编辑,因为这更多的是您已经做过的评论(Flatter没有StringBuilder),调整示例以适应可用的内容应该不会太难。我还建议使用第二个选项-我添加第一个选项只是因为不清楚您是否需要中间数组。@Timebreaker900“为什么它不在最后一个客户端添加逗号”-我假设您指的是第一个选项,但它们的工作原理基本相同:如果已找到选定的元素,则在新元素和已找到的元素之间添加一个逗号,如果尚未找到,则使用新元素。实际区别是(除了使用中间数组):选项1预先添加新元素,而选项2追加新元素,即选项1类似于
c
->
b,c
->
a,b,c
,而选项2类似于
a
->
a,b
->
a,b,c
。对于你的第一个问题:我本来可以这样做,但我不想让它变得复杂,因为它已经是了。^^^对于你的第二个问题问题:不,只有逗号。但是你的答案看起来也不错,我会尝试第二个,我会让你知道。StringBuilder的第二个示例也很有效。它实际上比另一个答案更好,因为它较短。但是我在Flutter中工作,所以StringBuilder不可用。因此我需要对其进行一些更改。我还有一个问题:为什么它不在最后一个客户端添加逗号?@Timebreaker900我拒绝了你的编辑,因为这更多的是你已经做过的评论(Flutter没有StringBuilder)调整示例以适应可用的内容应该不会太难。我还建议使用第二个选项-我添加第一个选项只是因为不清楚是否需要中间数组。@Timebreaker9