Array Checking in Java.

S

Sanny

I want to know is it possible to do Array Calculation in one step?

FIRST
_________

Say I have array[100]; I want to assign each of them a Value of 0; In
just 1 step.

Currently I use for loop to assign each array element. But I think
there should be some way to initialize all elements to 0.

Does java initializes all elements to 0. When we have not assigned or
give it a null Value?

SECOND
--------------

Further I use an Array like

xx[j]=99; Here I have to first verify that i & j are between 0-10;

So I use

if ((i>0)&&(i<10)&&(j>0)&&(j<10)) xx[j]=99;

But What I want is since Java itself checks out of Bounds, Instead of
Hanging when i<0 or i>10 It just skip it. So my Code will work just
like xx[j]=99; and when out of Bounds Ocur it just skip that
instead of hanging. Can it be done?

That is Java do not hang when out of bound happens but just skip it
and go to next iteration

Something like.

if (xx[j]=99;) === (Out of Bound) break; else proceed further. Can
this be done in java?

THIRD
--------

Is it possible to use Arrays as Sets. And do Union, Intersect, and
other things we do in Set Theory.

Say we have two arrays of Booleans.

BB1[] U BB2[] Gives Union of BB1 and BB2

BB1[] XOR BB2[] Give XOR for the Booleans?

Is it Possible?

Bye
Sanny
 
T

Thomas Kellerer

Sanny, 10.01.2008 10:38:
Say I have array[100]; I want to assign each of them a Value of 0; In
just 1 step.

int[] myarray = new int[100];

Java gurantees the initialization with 0.
Check the language specification.
SECOND
--------------
Something like.

if (xx[j]=99;) === (Out of Bound) break; else proceed further. Can
this be done in java?


You should catch the ArrayIndexOutOfBoundsException
No, you need to use a Set. They offer these methods.
 
S

Sanny

Say I have array[100]; I want to assign each of them a Value of 0; In
just 1 step.

int[] myarray = new int[100];

Java gurantees the initialization with 0.
Check the language specification.

What about char array?

char[] chararray = new char[100];

I want all of them to be ' '; Will they be done automatically? Can I
change the default parameter to something else?
if (xx[j]=99;) === (Out of Bound) break; else proceed further. Can
this be done in java?


You should catch the ArrayIndexOutOfBoundsException


Will it be as fast as checking just one condition without wasting much
time writing out the error? So that I can avoid Checking myself?
No, you need to use a Set. They offer these methods.

How to initialize a set?

Currently I have two array

int[] array1 = new int[100];
int[] array2 = new int[100];

How to convert them to Set and use Set Theory.

How fast will they work When I use Union/ XOR/ etc. Will they be
performed in one step? And will the speed improve?

Bye
Sanny
 
T

Thomas Kellerer

Sanny, 10.01.2008 11:18:
Say I have array[100]; I want to assign each of them a Value of 0; In
just 1 step.
int[] myarray = new int[100];

Java gurantees the initialization with 0.
Check the language specification.

What about char array?
Check the language specification it's all documented there:
if (xx[j]=99;) === (Out of Bound) break; else proceed further. Can
this be done in java?

You should catch the ArrayIndexOutOfBoundsException


Will it be as fast as checking just one condition without wasting much
time writing out the error? So that I can avoid Checking myself?


The bounds will always be checked by Java. There is no way to disable
this. Again read the language specs.
How to initialize a set?
Currently I have two array

int[] array1 = new int[100];
int[] array2 = new int[100];

How to convert them to Set and use Set Theory.
Check the Javadocs for the different implementations of Set.

How fast will they work When I use Union/ XOR/ etc. Will they be
performed in one step? And will the speed improve?

Definitely not "in one step". I have no idea if your speed will
"improve". Firstly because I have no idea what your current code does,
and secondly because I have no idea what you new code will do).

You will need to test that for yourself.

If we are talking about 100 items per set, I wouldn't be concerned about
the speed.

Thomas
 
L

Lars Enderin

Sanny skrev:
Say I have array[100]; I want to assign each of them a Value of 0; In
just 1 step.
int[] myarray = new int[100];

Java gurantees the initialization with 0.
Check the language specification.

What about char array?

char[] chararray = new char[100];

I want all of them to be ' '; Will they be done automatically? Can I
change the default parameter to something else?
See Arrays.fill(char[] a, char val):
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#fill(char[], char)
 
L

Lew

Lars said:
Sanny skrev:
Say I have array[100]; I want to assign each of them a Value of 0; In
just 1 step.
int[] myarray = new int[100];

Java gurantees the initialization with 0.
Check the language specification.

What about char array?

char[] chararray = new char[100];

I want all of them to be ' '; Will they be done automatically? Can I
change the default parameter to something else?
See Arrays.fill(char[] a, char val):
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#fill(char[], char)

To the OP: this is a convenience method - it isn't "one step" at the low
level, but it is "one step" in your application code.
 
L

Lew

Lars said:
Sanny skrev:
Say I have array[100]; I want to assign each of them a Value of 0; In
just 1 step.
int[] myarray = new int[100];

Java gurantees the initialization with 0.
Check the language specification.

What about char array?

char[] chararray = new char[100];

I want all of them to be ' '; Will they be done automatically? Can I
change the default parameter to something else?
See Arrays.fill(char[] a, char val):
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#fill(char[], char)

Arrays also has asList()
<http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#asList(T...)>
method, which can be combined with a Set's collection-arg constructor to
create the equivalent Set.

This example uses Character instead of char to obtain object-ness. When
building a Character array, avoid autoboxing from primitive [] inside a method
argument. That's why the Character [] is built prior to the Arrays.asList() call.

Character [] kars = { '0', 'Z', '-' };
Set <Character> karacters =
new HashSet <Character> ( Arrays.asList( kars ));
 
R

Roedy Green

But What I want is since Java itself checks out of Bounds, Instead of
Hanging when i<0 or i>10 It just skip it. So my Code will work just
like xx[j]=99; and when out of Bounds Ocur it just skip that
instead of hanging. Can it be done?


Yes, just catch the ArrayIndexOutOfBoundsException and print an error
message or whatever you planned to do in your if checks.
 
E

Eric Sosman

Sanny said:
[...]
Further I use an Array like

xx[j]=99; Here I have to first verify that i & j are between 0-10;

So I use

if ((i>0)&&(i<10)&&(j>0)&&(j<10)) xx[j]=99;

But What I want is since Java itself checks out of Bounds, Instead of
Hanging when i<0 or i>10 It just skip it. So my Code will work just
like xx[j]=99; and when out of Bounds Ocur it just skip that
instead of hanging. Can it be done?


Yes. You'll find a description of the technique in
"Effective Java" by Joshua Bloch -- given as an example
of something that is possible but that you should NOT do.
Here is Bloch's example code:

// Horrible abuse of exceptions. Don't ever do this!
try {
int i = 0;
while(true)
a[i++].f();
} catch(ArrayIndexOutOfBoundsException e) {
}

If you doubt the truth of the comment, I suggest you get
the book and read Bloch's reasons -- there's a lot of other
good stuff in that book, and it's worth having in any case.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top