how to create an array object

N

NickPick

I'd like to create an object array called test from the class
testclass. How exactly do I have to do this? The code below deosn't
work. What's wrong with it? Many thanks

public static void main(String[] args) {
// TODO code application logic here

testclass[] test = new testclass[10];

for (int x = 1; x <= 5; x++) {
test[x].check_status();
}
}
 
J

Joshua Cranmer

NickPick said:
I'd like to create an object array called test from the class
testclass. How exactly do I have to do this? The code below deosn't
work. What's wrong with it? Many thanks

Well, you haven't told us what is wrong, but I can guess.
public static void main(String[] args) {
// TODO code application logic here

testclass[] test = new testclass[10];

for (int x = 1; x <= 5; x++) {
test[x].check_status();
}
}

Problem 1: This does not compile. Specifically, the main needs to be in
a class of some sort, and we have no definition for `testclass' nor its
hypothetical check_status method.

Problem 2: Java coding standards highly recommend that class names begin
with a capital letter and that they should be in CamelCase.

Problem 3: The indexes of an array of length N go from 0 to N-1,
inclusive (or as Edgar Dijkstra would put it, 0 <= i < N).

Problem 4: Hardcoding the 5 is a good way to set yourself up for an
ArrayIndexOutOfBoundsException. Your for loop should therefore look more
like this:

for (int x = 0; x < test.length; x++)

Problem 5: You never initialized the array. All arrays are set to their
default values, which are 0 for numeric arrays, '0' for character
arrays, false for boolean arrays, and null for Object arrays. Therefore,
I am guessing that your problem is that you are getting a
NullPointerException. The way around this is to fill the array with
something first.
 
N

NickPick

NickPick said:
I'd like to create an object array called test from the class
testclass. How exactly do I have to do this? The code below deosn't
work. What's wrong with it? Many thanks

Well, you haven't told us what is wrong, but I can guess.
 public static void main(String[] args) {
        // TODO code application logic here
        testclass[] test = new testclass[10];
        for (int x = 1; x <= 5; x++) {
        test[x].check_status();
        }
    }

Problem 1: This does not compile. Specifically, the main needs to be in
a class of some sort, and we have no definition for `testclass' nor its
hypothetical check_status method.

Problem 2: Java coding standards highly recommend that class names begin
with a capital letter and that they should be in CamelCase.

Problem 3: The indexes of an array of length N go from 0 to N-1,
inclusive (or as Edgar Dijkstra would put it, 0 <= i < N).

Problem 4: Hardcoding the 5 is a good way to set yourself up for an
ArrayIndexOutOfBoundsException. Your for loop should therefore look more
like this:

for (int x = 0; x < test.length; x++)

Problem 5: You never initialized the array. All arrays are set to their
default values, which are 0 for numeric arrays, '0' for character
arrays, false for boolean arrays, and null for Object arrays. Therefore,
I am guessing that your problem is that you are getting a
NullPointerException. The way around this is to fill the array with
something first.

Thanks for your detailed reply, but it doesn't solve my problem yet
(it's my first day with Java).
I have defined a class named TestClass. The class contains a method
called check_status. Now I'd like to create 5 objects of that class.
The 5 objects should be an array called test[1..5]. How do I create
those class objects so that I can call their methods test
[x].check_status(); (where x is a number between 1 and 5, depending on
which object I want?

thanks
 
B

blueparty

NickPick said:
NickPick said:
I'd like to create an object array called test from the class
testclass. How exactly do I have to do this? The code below deosn't
work. What's wrong with it? Many thanks
Well, you haven't told us what is wrong, but I can guess.
public static void main(String[] args) {
// TODO code application logic here
testclass[] test = new testclass[10];
for (int x = 1; x <= 5; x++) {
test[x].check_status();
}
}
Problem 1: This does not compile. Specifically, the main needs to be in
a class of some sort, and we have no definition for `testclass' nor its
hypothetical check_status method.

Problem 2: Java coding standards highly recommend that class names begin
with a capital letter and that they should be in CamelCase.

Problem 3: The indexes of an array of length N go from 0 to N-1,
inclusive (or as Edgar Dijkstra would put it, 0 <= i < N).

Problem 4: Hardcoding the 5 is a good way to set yourself up for an
ArrayIndexOutOfBoundsException. Your for loop should therefore look more
like this:

for (int x = 0; x < test.length; x++)

Problem 5: You never initialized the array. All arrays are set to their
default values, which are 0 for numeric arrays, '0' for character
arrays, false for boolean arrays, and null for Object arrays. Therefore,
I am guessing that your problem is that you are getting a
NullPointerException. The way around this is to fill the array with
something first.

Thanks for your detailed reply, but it doesn't solve my problem yet
(it's my first day with Java).
I have defined a class named TestClass. The class contains a method
called check_status. Now I'd like to create 5 objects of that class.
The 5 objects should be an array called test[1..5]. How do I create
those class objects so that I can call their methods test
[x].check_status(); (where x is a number between 1 and 5, depending on
which object I want?

thanks

As Joshua said, you need to create those TestClass object before calling
their check_status() method, unless the method itself is static. To
create an object operator "new" is used most often. You can code:

test[x]=new TestClass();

B
 
E

Eric Sosman

NickPick said:
NickPick said:
I'd like to create an object array called test from the class
testclass. How exactly do I have to do this? The code below deosn't
work. What's wrong with it? Many thanks
Well, you haven't told us what is wrong, but I can guess.
public static void main(String[] args) {
// TODO code application logic here
testclass[] test = new testclass[10];
for (int x = 1; x <= 5; x++) {
test[x].check_status();
}
}
Problem 1: This does not compile. Specifically, the main needs to be in
a class of some sort, and we have no definition for `testclass' nor its
hypothetical check_status method.

Problem 2: Java coding standards highly recommend that class names begin
with a capital letter and that they should be in CamelCase.

Problem 3: The indexes of an array of length N go from 0 to N-1,
inclusive (or as Edgar Dijkstra would put it, 0 <= i < N).

Problem 4: Hardcoding the 5 is a good way to set yourself up for an
ArrayIndexOutOfBoundsException. Your for loop should therefore look more
like this:

for (int x = 0; x < test.length; x++)

Problem 5: You never initialized the array. All arrays are set to their
default values, which are 0 for numeric arrays, '0' for character
arrays, false for boolean arrays, and null for Object arrays. Therefore,
I am guessing that your problem is that you are getting a
NullPointerException. The way around this is to fill the array with
something first.
Thanks for your detailed reply, but it doesn't solve my problem yet
(it's my first day with Java).
I have defined a class named TestClass. The class contains a method
called check_status. Now I'd like to create 5 objects of that class.
The 5 objects should be an array called test[1..5]. How do I create
those class objects so that I can call their methods test
[x].check_status(); (where x is a number between 1 and 5, depending on
which object I want?

The first thing you must do is learn that Java is case-
sensitive, so that `TestClass' and `testclass' are different
things.

Next, you must learn the difference between an object and
an object reference. When you write

TestClass[] test = new TestClass[5];

you create an array object that can hold five references to
TestClass objects. But at the moment all five of them hold
the value `null', which refers to nothing at all. Why not?
Because you have created no TestClass objects for them to
refer to. Before you can use `test[2]', say, to access an
object you must create an object for it to refer to and fill
`test[2]' with a reference to that object, e.g.

test[2] = new TestClass("Psychology 101", 42);

Finally, you must learn that Java's arrays are indexed
from zero. The five slots in `test' are called `test[0]'
through `test[4]', *not* `test[1]' through `test[5]'. If
you actually want the latter, you need to create an array
with *six* elements

TestClass[] test = new TestClass[6];

and just ignore the `test[0]' slot that you're not using for
anything. (A wiser course is to learn to be comfortable with
zero-based array indices.)
 
L

Lew

NickPick said:
Thanks for your detailed reply, but it doesn't solve my problem yet
(it's my first day with Java).
I have defined a class named TestClass. The class contains a method
called check_status. Now I'd like to create 5 objects of that class.
The 5 objects should be an array called test[1..5]. How do I create
those class objects so that I can call their methods test
[x].check_status(); (where x is a number between 1 and 5,

It was already mentioned that Java array indexes start at 0, not 1, therefore
you want x as a number from 0 through 4, inclusive.
(or as Edgar Dijkstra would put it, 0 <= x < 5)

to paraphrase Joshua.
depending on which object I want?

There's no sense in putting the word "Class" in a class name. Starting the
identifier with an upper-case letter suffices to identify the fact that it's a
class name.

Method names should not contain underscores; instead use camel case (each word
part capitalized), with the first character lower case.

<example>
package test;
/** Class to test use of arrays. */
public class Test
{
/** Check the status.
* @return boolean <code>true</code> iff status is OK.
*/
public boolean checkStatus()
{
return true;
}

/** Entry point.
* @param args <code>String []</code> command-line arguments.
*/
public static void main( String [] args )
{
Test [] tests = new Test [5];
for ( int ix = 0; ix < tests.length; ++ix )
{
tests [ix] = new Test();
}

for ( Test test : tests )
{
if ( test.checkStatus() )
{
System.out.println( "pass" );
}
else
{
System.out.println( "fail" );
}
}
}
}
</example>

read and study
<http://java.sun.com/docs/books/tutorial/index.html>
 
R

Roedy Green

I'd like to create an object array called test from the class
testclass. How exactly do I have to do this? The code below deosn't
work. What's wrong with it? Many thanks

public static void main(String[] args) {
// TODO code application logic here

testclass[] test = new testclass[10];

for (int x = 1; x <= 5; x++) {
test[x].check_status();
}


See http://mindprod.com/jgloss/array.html
http://mindprod.com/jgloss/initialisation.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP
 
N

NickPick

I'd like to create an object array called test from the class
testclass. How exactly do I have to do this? The code below deosn't
work. What's wrong with it? Many thanks
public static void main(String[] args) {
       // TODO code application logic here
       testclass[] test = new testclass[10];
       for (int x = 1; x <= 5; x++) {
       test[x].check_status();
       }

Seehttp://mindprod.com/jgloss/array.htmlhttp://mindprod.com/jgloss/initialisation.html
--
Roedy Green Canadian Mind Productshttp://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP

splendid! Thanks!
 
L

Lew

NickPick said:
I'd like to create an object array called test from the class
testclass. How exactly do I have to do this? The code below deosn't
work. What's wrong with it? Many thanks
public static void main(String[] args) {
// TODO code application logic here
testclass[] test = new testclass[10];
for (int x = 1; x <= 5; x++) {
test[x].check_status();
}
Seehttp://mindprod.com/jgloss/array.htmlhttp://mindprod.com/jgloss/initialisation.html
--

Please do not quote sigs.
splendid! Thanks!

Have you read
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.10>
or
<http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html>
?
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top