• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array  RSS feed

 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
Suppose I have an array

whereby Singer has members like title,year,price;

am I rite to do it in the following manner?

Singer CD[] = new Singer [30];
Singer tmpCD = new Singer();

for(i=0; i<10; i++)
{
tmpCD.title = 'retrieve info from file';
tmpCD.year = 'retrieve info from file';
tmpCD.price = 'retrieve info from file'
CD[i] = tmpCD;
}

or jus

Singer CD[] = new Singer [30];
Singer tmpCD;

for(i=0; i<10; i++)
{
tmpCD = new Singer();
tmpCD.title = 'retrieve info from file' ;
tmpCD.year = 'retrieve info from file';
tmpCD.price = 'retrieve info from file';
CD[i] = tmpCD;
}
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go with...



But if you have repetitive use of this functionality, I suggest you to create a Album class that will hold all objects Title class.
 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singer CD[] = new Singer [30];
Singer tmpCD;

for(i=0; i<10; i++)
{
tmpCD = new Singer();
tmpCD.title = 'retrieve info from file' ;
tmpCD.year = 'retrieve info from file';
tmpCD.price = 'retrieve info from file';
CD[i] = tmpCD;
}

do I need Singer CD[] = new Singer [30]; ??? since I have tmpCD = new Singer() which I have already allocated memory in it
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you do need the Singer CD[] = new Singer [30];

this allocates space to hold the REFERENCES to the objects. the

tmpCD = new Singer();

allocates space for the new objects. without the first part, you can create all the objects, but you have nothing holding onto them (referring to them). when you did the CD[i] = tmpCD, you'd get an error.
 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ic,thank u every1 for participating in this thread.
may i ask 1 more qns, which 1 is better?

should I create object of tmpCD everytime and assign to Singer array??

Singer CD[] = new Singer [30];
.......
.......
while(...)
{
tmpCD = new Singer();
.......
.......
CD[i] = tmpCD;
}

or retrieve the data from tmpCD and set it to CD array

Singer CD[] = new Singer [30];
tmpCD = new Singer();

while(..)
{
.......
CD[i].setTitle(tmpCD.getTitle());
.......
}
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or retrieve the data from tmpCD and set it to CD array

Singer CD[] = new Singer [30];
tmpCD = new Singer();

while(..)
{
.......
CD[i].setTitle(tmpCD.getTitle());
.......
}


When you create an array like this,

do you know what is in each index of the array? For instance just after that line what would be in CD[4]?

Put some sysouts in your while loop and see what is in the array after creation. Once you do that you'll be able to answer your own question.

Just as a side note I hate the name CD for your array. Typically you'd start variable names with a lower case.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hentay has a valid point. the convention in java is for variables to start with a lowercase letter, and each subsequent word has it's first letter capitalized...

String thisIsMyString;
Foo fooBar;

etc. Note that this is required, it's just a convention. but it's pretty much a universally accepted convention, and just about anyplace you work will expect it. it's a good habit to get into now.

now as to which is the better way...

when you create an array like this:

Singer CD[] = new Singer [30];

you are effectively creating a holder for objects. think of it as building 30 garages for your future cars. you don't have any cars yet, just a place to put them. all the garages are empty. you need to buy 30 new cars to put into the garages.

similarly, you need to create 30 new singers, and put each one into it's spot in the array. if you only create one, then you would end up with 30 elements in your array all pointing to the same object.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!