Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #16800 > unrolled thread
| Started by | bob smith <bob@coolfone.comze.com> |
|---|---|
| First post | 2012-07-31 12:58 -0700 |
| Last post | 2012-07-31 14:08 -0700 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | bob smith <bob@coolfone.comze.com> |
|---|---|
| Date | 2012-07-31 12:58 -0700 |
| Subject | initialize an array of booleans |
| Message-ID | <1db13953-d741-4f7d-ad9a-27ca5b194884@googlegroups.com> |
Is there any easy way to initialize an array of booleans like this? boolean b[] = new boolean(false)[100];
[toc] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-07-31 13:08 -0700 |
| Message-ID | <9ff73f81-f913-40a6-995b-0f7704623b6d@googlegroups.com> |
| In reply to | #16800 |
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
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2012-07-31 16:12 -0400 |
| Message-ID | <jv9e7g$qc6$1@dont-email.me> |
| In reply to | #16800 |
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
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-07-31 14:08 -0700 |
| Message-ID | <i8ig18hpr711i8fe22pvq4mh0tl7gep5on@4ax.com> |
| In reply to | #16800 |
On Tue, 31 Jul 2012 12:58:58 -0700 (PDT), bob smith <bob@coolfone.comze.com> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web