Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!53ab2750!not-for-mail From: "Eric Sosman" Subject: Re: initialize an array of booleans Message-ID: <50197005.55892.calajapr@time.synchro.net> X-Comment-To: bob smith Newsgroups: comp.lang.java.programmer In-Reply-To: <50197002.55874.calajapr@time.synchro.net> References: <50197002.55874.calajapr@time.synchro.net> X-FTN-AREA: COMP.LANG.JAVA.PROGRAMMER X-FTN-MSGID: 1:261/38 949e4ae2 X-FTN-REPLY: 1:261/38 1a997b26 Content-Type: text/plain; charset=IBM437 Content-Transfer-Encoding: 8bit X-Gateway: time.synchro.net [Synchronet 3.16a-Win32 NewsLink 1.98] Lines: 38 Date: Wed, 01 Aug 2012 18:09:34 GMT NNTP-Posting-Host: 69.21.70.65 X-Complaints-To: news@tds.net X-Trace: newsreading01.news.tds.net 1343844574 69.21.70.65 (Wed, 01 Aug 2012 13:09:34 CDT) NNTP-Posting-Date: Wed, 01 Aug 2012 13:09:34 CDT Organization: tds.net Xref: csiph.com comp.lang.java.programmer:16924 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[i] = true; ... or with the prepackaged array-filling method: boolean[] b = new boolean[100]; java.util.Arrays.fill(b, true); -- Eric Sosman esosman@ieee-dot-org.invalid --- 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