views:

79

answers:

7

Is there a way to do the following at the same time?

static final int UN = 0; // uninitialized nodes
int[] arr;

// ... code ...

arr = new int[size];
for (int i = 0; i < 5; i++) {
    arr[i] = UN;
}

Basically, I want to declare arr once I know what its size will be and initialize it to UN without having to loop. So something like this:

int[] arr = new int[size] = UN;

Is this possible?

Thanks.

A: 

Oops, read your question better:

You can init an array like so

int[] arr = new int[] {UN, UN, UN, UN, UN};

But ofcourse, if you don't know the size at compile time, then you have to do the for loop. The second technique is not possible.

Java Drinker
Only if you know what size it's going to be, and that size will never change.
Paul Tomblin
I don't know the size of it to begin with... so I can't do `{ UN, UN, UN }`. I will eventually know the size but it will be in a variable
Hristo
A: 

No.

Next question?

Paul Tomblin
@Paul... Ok. Well... is it possible to initialize an array of size n in O(1)?
Hristo
@Hristo: No... why would you think that?
Vuntic
@Vuntic... In C you can do `int[5] arr = 0;` right? Isn't that O(1)?
Hristo
@Hristo: How could it possibly be? It's still initializing the memory to the specified values behind the scenes, no matter how pretty the syntax is.
Vuntic
Good point... I wasn't thinking about memory. Thanks
Hristo
+2  A: 
Arrays.fill(arr, UN);
Satyajit
Although you should be aware that behind the scenes, that's going to do the same loop you were trying to avoid.
Paul Tomblin
-1 I think the point is to avoid the extra code. Also, you can't use that to do it in a single statement because it doesn't return the array.
Vuntic
A: 
int arr[] = { 0, 0, 0, 0, 0 };
tpdi
@tpdi... I didn't downvote you. Sorry for that. I hate it when it happens to me. But as far as your answer goes, I know that... the problem is I don't know the size, it is in a variable.
Hristo
Thanks for telling me you didn't down vote me, but no worries. Yes, if you don't know the size, you have to initialize in a loop, *unless* the number you want to initialize to is zero -- as zero is the default value for int. (Not for Integer; it's defualt is null)
tpdi
A: 

No, not with the standard libraries. If you write your own functions, though, you can easily do so in a single statement (not instruction; those are different). Mine looks like String[][] strings = Arrayu.fill(new String[x][y], "");

Here's a link. There's some junk in there too, though; I just posted a copy of the current source directly without cleaning it up.

Vuntic
This looks interesting. Does this still loop through the indices behind the scenes?
Hristo
Of course. That's the only way it can be done. (btw: java.lang.Arrays loops through the indices too, but it also does bounds checking even when you just call `Arrays.fill(arr, UN)`. It's kinda stupid like that.)
Vuntic
@Vuntic. Thanks for you answers. I won't use this `Arrayu` since I'm trying to avoid extra "stuff" and I'll resort to my for loop. But your answer is best so far... so I'll accept it.
Hristo
+1  A: 

You don't need to initialize them with 0. An int defaults to 0 already.

Just

int[] array = new int[size];

is enough. It gives you an array of zeroes of the given length. If it were an Integer[], it would have been an array of nulls.

BalusC
@BalusC... thanks. I actually asked another question that this is the answer for. So thanks :)
Hristo
+1  A: 

Well, in the case of objects (or primitives with autoboxing) you can do the following:

int count = 20;
final int UN = 0;
Integer[] values = Collections.nCopies(count, UN).toArray(new Integer[count]);

The downsides are that you have to use the object forms of the primitives (since the Collections must be of objects) and a separate List will be constructed and then thrown away. This would allow you to create the array as one statement however.

mwooten.dev