The Artima Developer Community
Sponsored Link

Java Answers Forum
Create an array of objects

1 reply on 1 page. Most recent reply: May 2, 2003 8:05 AM by Erik Price

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
niffin

Posts: 2
Nickname: niffin
Registered: May, 2003

Create an array of objects Posted: May 2, 2003 7:07 AM
Reply to this message Reply
Advertisement
Hi

I have written some code to create objects called Train that have 2 integers.
public class Train
{
int arrivaltime;
int departtime;
Train(int d, int a){
arrivaltime=a;
departtime=d;
}
}

I have then tried to make an array of Train objects.
public class Timetable
{
public static void main(String[]args)

{String inputa, inputd;
int a, d, inputar, inputdt, inc;
a=0;
d=0;
inc=0;
char ch;
Train trains[]; //i think this makes an array of train objects.

trains = new Train[10]; //i hope this intialises the array.



This all seems to work ok but I am then not sure how to assign the 2 integers to a train.

E.g. trains[0] //i want the first integer to be 4 and second to be 5 for example.

How do i assign data to the array???


Please Help


Niffin


Erik Price

Posts: 39
Nickname: erikprice
Registered: Mar, 2003

Re: Create an array of objects Posted: May 2, 2003 8:05 AM
Reply to this message Reply
Hi Niffin,


The problem is here:

Train trains[]; //i think this makes an array of train objects.

trains = new Train[10]; //i hope this intialises the array.

This doesn't do what you think it does. The first line declares an array of Train objects named "trains". That part is fine, but it doesn't actually make the array. It just says "whenever I use the reference 'trains' I'm referring to an array of Train objects".

The second line is where the array is actually created and initialized. You're basically telling the compiler to allocate the memory required to store a 10-element array of Train objects. But you haven't actually put any Train objects into the array. (Each element of "trains" is still null.)

What you need to do now is actually add instances of Train objects to the array by directly assigning them to the individual elements. Here is one, kind of long, way to do it:

trains[0] = new Train(5, 8);
trains[1] = new Train(4, 32);
trains[2] = new Train(88, 222);
...


Another way to do it is to loop through the array and create a new Train object for each element:


for (int i = 0; i < trains.length; i++) {
trains[i] = new Train(5, 654);
}


The lesson here is that when you create an array of non-primitives (in other words, an array of objects), the array is initialized such that each element is null. You still have to manually add instances to the array's elements somehow.

Flat View: This topic has 1 reply on 1 page
Topic: Problem in Java Chat Application Previous Topic   Next Topic Topic: small problem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use