Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set all values of ArrayList<Boolean> to false on instantiation

Tags:

Is there an ease way to create an ArrayList<Boolean> using Java and have them initially all set to false without looping through and assigning each to false?

like image 989
Allan Macmillan Avatar asked Dec 16 '13 16:12

Allan Macmillan


People also ask

How do you initialize a boolean array as false?

Either use boolean[] instead so that all values defaults to false : boolean[] array = new boolean[size]; Or use Arrays#fill() to fill the entire array with Boolean.

How do you set all elements in an array to false?

boolean a[]= new boolean[nums. length]; Arrays. fill(a, false); // this will help you fill array of boolean with false.

How would you declare an ArrayList so that it can store true or false values?

How could you declare an ArrayList so that it can store true or false values? ArrayList<Boolean> arrList = new ArrayList<>(); What is an array? an indexed container that holds a set of values of a single type.

How do you declare a boolean ArrayList?

boolean[] arr = new boolean[10]; This will auto-initialize to false since boolean 's default value is false .


1 Answers

Do like this

List<Boolean> list=new ArrayList<Boolean>(Arrays.asList(new Boolean[10])); Collections.fill(list, Boolean.TRUE); 
like image 55
Prabhakaran Ramaswamy Avatar answered Nov 01 '22 14:11

Prabhakaran Ramaswamy