2016-10-07 33 views
0

我有一个boolean arraylist充满了100个错误。更改值在一个数组列表

我想更改数组中的每个x值。那么x = 1,如果x = 2,其他每个值都会改变[false,true,false,true,false],每两个值都会改变。我一直在努力工作几个小时,但仍然无法得到它。在这个网站上询问是我最后的选择。

这是我有...

public void changeValues(int x){ 

    int d = x + 1; 
    x-=1; 
    int i = 0; 

    while (x <= arrayList.size()){ 

     if (arrayList.get(x) == false){ 

      arrayList.add(x,true); 
     } else { 

      arrayList.add(x,false); 
     } 
     x+=d; 
     if(x == arrayList.size()){ 
      break; 
     } 
    } 
    System.out.print(arrayList); 
} 
+2

你的意思是每两个值都会改变吗?假的,假的,真的,真的,假的,假的? –

+0

请让你的问题陈述清楚! –

+2

此代码不会更改列表中的现有元素,而是将新元素添加到列表中。 – paisanco

回答

2

首先,add指定索引之后添加元素,而不是设置它。

其次,你会混合导致完全错误结果的x和循环索引变量(我假设它抛出ArrayIndexOutOfBoundException)。

的实现其实很简单:

for (int i = x; i < arrayList.size(); i += x+1) { 
    arrayList.set(i, true); 
    // or arrayList.set(i, !arrayList.get(i)) if you want to negate the value 
} 
0

使用SET而不是相加。

添加将指定元素附加到此列表的末尾或指定的索引处。

设置将取代它。

0

你必须修改值不增加新的val.Use下面的代码获取输出中提到的格式

public void changeValues(int x){ 
     ArrayList<Boolean> list = new ArrayList<Boolean>(); 
     //From question -> that is filled with 100 falses. 
     list.add(false); 

     int listSize = list.size(); 
     for (int i = x; i < listSize; i += x + 1) { 
      list.set(i, true);   //Works for x=1 
      if (x == 2 && i+1 < listSize) { 
       list.set(i + 1, true); //Works for x=2 
      } 
     } 
     System.out.println(list); 
    } 

输出为x=1

[false, true, false, true, false] 

输出为x=2

[false, false, true, true, false] 
1
public void changeValues(int x){ 

    int length = arrayList.size(); 

    Collections.fill(arrayList, Boolean.TRUE); 

    for(int a = 0; a < length; a += x){ 
     arrayList.set(a, Boolean.FALSE); 
    } 


    System.out.print(arrayList); 
} 

然而要测试代码,但我想这就是你要找的。希望它能帮助:)

0

使用Java 8中,您可以通过检查做出解决方案,如果在index % x == 0IntStream

final int size = 100; 
final int gap = x + 1; 
List<Boolean> list = IntStream.rangeClosed(1, size) 
           .mapToObj(num -> num % gap == 0) 
           .collect(Collectors.toList()); 
System.out.println(list); 

Ideone Demo

0

(我建议总是张贴MCVE

import java.util.ArrayList; 
import java.util.List; 

public class Test{ 

     final static int arrayListSize = 10; 
     static List<Boolean> arrayList = new ArrayList<Boolean>(); 

     public static void main(String[] args){ 

      changeValues(3); 
     } 

     public static void changeValues(int changeValueStep){ 

      //add check that changeValueStep is nut 0. 

      boolean value = false; 
      for(int i = 0; i< arrayListSize ; i++) { 

       arrayList.add(value); 
       //change value test 
       if(((i +1) % changeValueStep) == 0) { 
        value = !value; 
       } 
      } 
     } 
} 
0

请检查这个,我认为它会像你要找的东西一样工作:

public class ChangeValue{ 
     public static void main(String[] args) { 
       List arrayList = new ArrayList<Boolean>(); 
       for (int i = 0; i < 100; i++) { 
        arrayList.add(false); 
       } 
       System.out.println("Before:" + arrayList); 
       for (int i = 0; i < 100; i++) { 
        arrayList.add(i,true); 
       } 
       new ChangeValue().changeValues(1, arrayList); 

      } 

      public void changeValues(int x, List<Boolean> arrayList) { 
       int d = x+1; 
       x -= x; 
       System.out.println("ff:" + arrayList.size()); 
       while (x <= arrayList.size()) { 
        if (arrayList.get(x) == true) { 
         arrayList.add(x, false); 
        } 
        x += d; 
        if (x == arrayList.size()) { 
         break; 
        } 
       } 
       System.out.print("After:" + arrayList); 
      } 
     public static void main(String[] args) { 
       List arrayList = new ArrayList<Boolean>(); 
       for (int i = 0; i < 100; i++) { 
        arrayList.add(false); 
       } 
       System.out.println("Before:" + arrayList); 
       for (int i = 0; i < 100; i++) { 
        arrayList.add(i,true); 
       } 
       new StateFlowerBird().changeValues(1, arrayList); 

      } 

      public void changeValues(int x, List<Boolean> arrayList) { 
       int d = x+1; 
       x -= x; 
       System.out.println("ff:" + arrayList.size()); 
       while (x <= arrayList.size()) { 
        if (arrayList.get(x) == true) { 
         arrayList.add(x, false); 
        } 
        x += d; 
        if (x == arrayList.size()) { 
         break; 
        } 
       } 
       System.out.print("After:" + arrayList); 
      } 

    } 
相关问题