Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #16894
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: initialize an array of booleans |
| Date | Tue, 31 Jul 2012 16:12:30 -0400 |
| Organization | A noiseless patient Spider |
| Lines | 30 |
| Message-ID | <jv9e7g$qc6$1@dont-email.me> (permalink) |
| References | <1db13953-d741-4f7d-ad9a-27ca5b194884@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Tue, 31 Jul 2012 20:12:32 +0000 (UTC) |
| Injection-Info | mx04.eternal-september.org; posting-host="d3779b2c4a3397eb5709eec94bad057a"; logging-data="27014"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fNWn3dCvJp7KNTCK0O8RJ" |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 |
| In-Reply-To | <1db13953-d741-4f7d-ad9a-27ca5b194884@googlegroups.com> |
| Cancel-Lock | sha1:ZrIt25CaNNZwMVmIjxL6ddHAOpc= |
| Xref | csiph.com comp.lang.java.programmer:16894 |
Show key headers only | View raw
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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
initialize an array of booleans bob smith <bob@coolfone.comze.com> - 2012-07-31 12:58 -0700 Re: initialize an array of booleans Lew <lewbloch@gmail.com> - 2012-07-31 13:08 -0700 Re: initialize an array of booleans Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-31 16:12 -0400 Re: initialize an array of booleans Roedy Green <see_website@mindprod.com.invalid> - 2012-07-31 14:08 -0700
csiph-web