Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

Array Checking in Java.

Thread view: 
Sanny - 10 Jan 2008 09:38 GMT
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[i][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[i][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[i][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[i][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
Thomas Kellerer - 10 Jan 2008 10:08 GMT
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[i][j]=99;) === (Out of Bound) break; else proceed further. Can
> this be done in java?

You should catch the ArrayIndexOutOfBoundsException

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

No, you need to use a Set. They offer these methods.
Sanny - 10 Jan 2008 10:18 GMT
> > Say I have array[100]; I want to assign each of them a Value of 0; In
> > just 1 step.
[quoted text clipped - 3 lines]
> 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?

> > SECOND
> > --------------
[quoted text clipped - 4 lines]
>
> 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?

> > THIRD
> > --------
[quoted text clipped - 3 lines]
>
> 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
Thomas Kellerer - 10 Jan 2008 10:49 GMT
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.
[quoted text clipped - 4 lines]
>
> What about char array?
Check the language specification it's all documented there:
<http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html>

>>> if (xx[i][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.

>>> Is it possible to use Arrays as Sets. And do Union, Intersect, and
>>> other things we do in Set Theory.
>> 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.
Check the Javadocs for the different implementations of Set.

Basically it's: Set<Integer> set1 = new HashSet<Integer>(100) but that
will not add any entries to the set. As I said: you have to read the docs.

> 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
Lars Enderin - 10 Jan 2008 11:00 GMT
Sanny skrev:
>>> Say I have array[100]; I want to assign each of them a Value of 0; In
>>> just 1 step.
[quoted text clipped - 9 lines]
> 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[],%20char)
Lew - 10 Jan 2008 13:53 GMT
> Sanny skrev:
>>>> Say I have array[100]; I want to assign each of them a Value of 0; In
[quoted text clipped - 13 lines]
> See Arrays.fill(char[] a, char val):
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#fill(char[],%20char)

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.

Signature

Lew

Lew - 10 Jan 2008 14:12 GMT
> Sanny skrev:
>>>> Say I have array[100]; I want to assign each of them a Value of 0; In
[quoted text clipped - 13 lines]
> See Arrays.fill(char[] a, char val):
> http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arrays.html#fill(char[],%20char)

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 ));

Signature

Lew

Roedy Green - 10 Jan 2008 14:41 GMT
On Thu, 10 Jan 2008 01:38:47 -0800 (PST), Sanny
<softtanks@hotmail.com> wrote, quoted or indirectly quoted someone who
said :

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

see http://mindprod.com/jgloss/array.html
particularly
http://mindprod.com/jgloss/array.html#METHODS
where it mentions fill.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 10 Jan 2008 14:43 GMT
On Thu, 10 Jan 2008 01:38:47 -0800 (PST), Sanny
<softtanks@hotmail.com> wrote, quoted or indirectly quoted someone who
said :

>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[i][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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 10 Jan 2008 14:45 GMT
On Thu, 10 Jan 2008 01:38:47 -0800 (PST), Sanny
<softtanks@hotmail.com> wrote, quoted or indirectly quoted someone who
said :

>BB1[] U BB2[] Gives Union of BB1 and BB2
>
>BB1[] XOR BB2[] Give XOR for the Booleans?

no.  Have a look at http://mindprod.com/jgloss/enumset.html for
mathematical set operations.

You might also look at the Collections that implement the Set
interface. see http://mindprod.com/jgloss/set.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Eric Sosman - 11 Jan 2008 14:44 GMT
> [...]
> Further I use an Array like
[quoted text clipped - 9 lines]
> like xx[i][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.

Signature

Eric Sosman
esosman@ieee-dot-org.invalid



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.