PDA

View Full Version : Java Refresher Questions


jhebda
11-05-2002, 11:39 PM
Hi,

I have not used Java in about four years, and when I did use it ages ago, it was not much.

I am attempting to write some code to test a reinforcement learning algorithm and am having a little trouble remembering some basic syntax rules. Can someone help with the following questions if possible:

-When initializing an array in Java, how can I set all array values NULL? Right from the start i'll need to compare them against integer values, both negative and positive, so would like to initialize them as null somehow if possible.

-Is there a built in Java function to create random numbers within a certain range? (I know I can use a rand function and scale it accordingly), but I was wondering if there was a built in function to create a random integer, say, between 0 and 4 inclusive.

Thanks for your help!

John Hebda

Kentaro
11-06-2002, 12:10 AM
I'm new to Java myself so I'm not sure about initializing arrays but I know a bit about creating random numbers.

There's a class in the java.util package called Random. You can create an instance of the class, store it in an object variable, and invoke the nextInt(int n) method. That generates a random number from 0 to n inclusive, as far as I know.

Read more about it here!
java.sun.com/j2se/1.4.1/docs/api/java/util/Random.html

EDIT: Oh duh... that's right. n is not included. =)
Thanks Dru!

inkedmn
11-06-2002, 01:47 AM
if you create a new array without specifying the members, it will default to null (for a String array at least)...

like this:

public class Example {
public static void main(String[] args) {
String[] myArray = new String[10];
for(int i = 0; i < 10; i++) {
System.out.println(myArray[i]);
}
}
}


and here's the output:


D:\development\java_practice\myjava>java Example
null
null
null
null
null
null
null
null
null
null


hope this helps

[edit]

my mistake, i glossed over the part of your post that said you were working with an int array. i changed up my code and, unless specified otherwise, the values default to 0.

i just tried to initialize an int to null and i got a compiler error, so i don't think ints can be set to null

sorry :)

stuka
11-06-2002, 02:56 AM
inkedmn: ints can't be null, but Integers can, cuz ints are primitives, but Integers are (pointers) references.

Dru Lee Parsec
11-06-2002, 12:10 PM
If you changed the array declaration in Inkedmn's code to this

int[] myArray = new int[10];

You'd get a list of zeros instead of nulls.

Re: Random ints. Use the java.util.Random class with this method:


public int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between
0 (inclusive) and the specified value (exclusive).


The first thing I'd suggest is to download not only the JDK but the full Java documentation in html format. I live with the Java docs as my home page on my browser. Any time I wonder "How do I do that?" I start to browse the API docs. In Java there's usually a method call to do exactly what you need, or something very similar.

You may also find Bruce Eckel's book "Thinking In Java" useful. You can get it as html for free here http://64.78.49.204/

Good luck.

stuka
11-06-2002, 12:51 PM
Dru Lee: what, no suggestion to dl Jext too? :D