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 / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

intializing object arrays

Thread view: 
Kenneth - 08 Dec 2004 02:53 GMT
i have this code that is not working but I do not know what it is.

here is the code;

        // Create space for stock objects.
        Share[] stock = new Share[20];

        for(int x = 0; x<stock.length; x++)
        {
            stock[x].price = x;
            System.out.println(stock[x].price);
        }

        class stock(){

            public int price;

            public stock(){
              this.price = 30;
            }

        }

It should output 20 different numbers, but nothing outputs.
Ryan Stewart - 08 Dec 2004 03:05 GMT
>i have this code that is not working but I do not know what it is.
>
[quoted text clipped - 5 lines]
> for(int x = 0; x<stock.length; x++)
> {
stock[x] = new Share();

> stock[x].price = x;
> System.out.println(stock[x].price);
[quoted text clipped - 11 lines]
>
> It should output 20 different numbers, but nothing outputs.

It shouldn't output anything. It shouldn't even compile. Try posting real
code. See
http://www.physci.org/codes/sscce.jsp
Rob - 08 Dec 2004 03:09 GMT
> i have this code that is not working but I do not know what it is.
>
[quoted text clipped - 20 lines]
>
> It should output 20 different numbers, but nothing outputs.

I think your stock class needs to be a Share class.

Then,  you'd go

for(int i = 0; i < stock.length; i++)
{
stock[x] = new Share();
stock[x].price = x;
}

-rob
Rob - 08 Dec 2004 06:18 GMT
>> i have this code that is not working but I do not know what it is.
>>
[quoted text clipped - 32 lines]
>
> -rob

Oh, and by 'x' I meant 'i'
-rob


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



©2008 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.