Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > initialize an array of booleans

Reply
Thread Tools

initialize an array of booleans

 
 
bob smith
Guest
Posts: n/a
 
      08-01-2012
From: bob smith <(E-Mail Removed)>

Is there any easy way to initialize an array of booleans like this?

boolean b[] = new boolean(false)[100];

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-01-2012
To: bob smith
From: Eric Sosman <(E-Mail Removed)>

On 7/31/2012 3:58 PM, bob smith wrote:
> Is there any easy way to initialize an array of booleans like this?
>
> boolean b[] = new boolean(false)[100];


Sure: `boolean b[] = new boolean[100];' will do it, although
`boolean[] b = new boolean[100];' is usually considered better style.

... but that's just because a freshly-created boolean is `false'
until and unless it's given another value, just as a freshly-created int is
zero. To initialize with a lot of `true's you could write

boolean[] b = new boolean[]{true,true,true, /*etc.*/};

.... but that would be tedious, error-prone, and inefficient (you'd be dismayed
at the number of byte code instructions generated). It'd be better to use a
loop, either in open code:

boolean[] b = new boolean[100];
for (int i = 0; i < b.length; ++i)
b[i] = true;

.... or with the prepackaged array-filling method:

boolean[] b = new boolean[100];
java.util.Arrays.fill(b, true);

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      08-01-2012
To: bob smith
From: Lew <(E-Mail Removed)>

On Tuesday, July 31, 2012 12:58:58 PM UTC-7, bob smith wrote:
> Is there any easy way to initialize an array of booleans like this?
>
>
>
> boolean b[] = new boolean(false)[100];


boolean b[] = new boolean [100];

<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.10.1>

If you want to initialize array elements to a value that is not the default you
have to loop.

Or use the 'java.util.Arrays' class.
<http://docs.oracle.com/javase/7/docs...l#fill(boolean[],
boolean)>

Every Java programmer should know the collections framework and key utility
classes like 'Arrays'.

--
Lew

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-01-2012
To: bob smith
From: Roedy Green <(E-Mail Removed)>

On Tue, 31 Jul 2012 12:58:58 -0700 (PDT), bob smith
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>Is there any easy way to initialize an array of booleans like this?
>
>boolean b[] = new boolean(false)[100];


for basic info on arrays including initialisation, see
http://mindprod.com/jgloss/array.html

For this kind of info, a basic text book, even an out-of-date free one is
invaluable.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The greatest shortcoming of the human race is our inability to understand the
exponential function.
~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)
http://www.youtube.com/watch?v=F-QA2rkpBSY

--- BBBS/Li6 v4.10 Dada-1
* Origin: Prism bbs (1:261/3
--- Synchronet 3.16a-Win32 NewsLink 1.98
Time Warp of the Future BBS - telnet://time.synchro.net:24
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialize an array of booleans bob smith Java 3 07-31-2012 09:08 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
array of booleans zeus2@hotmail.com Java 4 10-01-2005 05:29 PM
g++: integers as booleans, no warning? Martin Herbert Dietze C++ 9 02-18-2005 09:03 AM
Booleans and comparison results Roman Suzi Python 0 06-24-2003 03:19 PM



Advertisments