initialize an array of booleans

Discussion in 'Java' started by bob smith, Aug 1, 2012.

  1. bob smith

    bob smith Guest

    From: bob smith <>

    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/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    bob smith, Aug 1, 2012
    #1
    1. Advertising

  2. bob smith

    Eric Sosman Guest

    To: bob smith
    From: Eric Sosman <>

    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 = true;

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

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

    --
    Eric Sosman
    d

    --- BBBS/Li6 v4.10 Dada-1
    * Origin: Prism bbs (1:261/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    Eric Sosman, Aug 1, 2012
    #2
    1. Advertising

  3. bob smith

    Lew Guest

    To: bob smith
    From: Lew <>

    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/api/java/util/Arrays.html#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/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    Lew, Aug 1, 2012
    #3
  4. bob smith

    Roedy Green Guest

    To: bob smith
    From: Roedy Green <>

    On Tue, 31 Jul 2012 12:58:58 -0700 (PDT), bob smith
    <> 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/38)
    --- Synchronet 3.16a-Win32 NewsLink 1.98
    Time Warp of the Future BBS - telnet://time.synchro.net:24
     
    Roedy Green, Aug 1, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. array of booleans

    , Oct 1, 2005, in forum: Java
    Replies:
    4
    Views:
    567
  2. Martin Herbert Dietze

    g++: integers as booleans, no warning?

    Martin Herbert Dietze, Feb 17, 2005, in forum: C++
    Replies:
    9
    Views:
    492
    Karl Heinz Buchegger
    Feb 18, 2005
  3. Roman Suzi

    Booleans and comparison results

    Roman Suzi, Jun 24, 2003, in forum: Python
    Replies:
    0
    Views:
    720
    Roman Suzi
    Jun 24, 2003
  4. =?ISO-8859-1?Q?Gerhard_H=E4ring?=

    Re: Booleans and comparison results

    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jun 24, 2003, in forum: Python
    Replies:
    0
    Views:
    711
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Jun 24, 2003
  5. bob smith

    initialize an array of booleans

    bob smith, Jul 31, 2012, in forum: Java
    Replies:
    3
    Views:
    199
    Roedy Green
    Jul 31, 2012
Loading...

Share This Page