Felix Doe
Felix Doe

Reputation: 299

Change Values In an arraylist

I have a boolean arraylist that is filled with 100 falses.

I want to change every x value in an arraylist. So is x = 1, every other values would change [false,true,false,true,false] if x = 2, every two value would change. I've been working on this for hours and I still can't get it. Asking on this website was my last option.

Here is what I have...

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

Upvotes: 0

Views: 5566

Answers (7)

Jay Prakash
Jay Prakash

Reputation: 805

Please check this , i think it will work like what you are looking for:

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

    }

Upvotes: 0

c0der
c0der

Reputation: 18812

(I recommend always post an 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;
                }
            }
         }
}

Upvotes: 0

castletheperson
castletheperson

Reputation: 33506

Using Java 8, you can make a solution by checking if index % x == 0 in an IntStream:

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

Upvotes: 0

Prasanna Kumar H A
Prasanna Kumar H A

Reputation: 3431

You have to modify the value not to add new val.Use following code to get the output in mentioned format

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

Output for x=1

[false, true, false, true, false]

Output for x=2

[false, false, true, true, false]

Upvotes: 0

Rishi Goel
Rishi Goel

Reputation: 680

Use Set instead of add.

Add will Appends the specified element to the end of this list or at the specified index.

Set will replace it.

Upvotes: 0

DriLLFreAK100
DriLLFreAK100

Reputation: 1606

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

Yet to test out the codes but I guess this is what you are looking for. Hope it helps :)

Upvotes: 1

Zbynek Vyskovsky - kvr000
Zbynek Vyskovsky - kvr000

Reputation: 18865

First, add adds the element after the specified index, not setting it.

Secondly you kind of mix the x and loop index variable which leads to completely wrong result (I assume it throw ArrayIndexOutOfBoundException).

The implementation is actually quite simple:

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
}

Upvotes: 2

Related Questions