![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 21
Rep Power: 0
![]() |
creating an array of an object/class?
I can't seem to do it, I'm assuming its just like creating an array of anything else right?
So if I have one class: [code] import java.util.*; public class abcd { public static void main(String [] args) { abc[] link = new abc[5]; link[0].number(); } } and the other one its supposed to create an array of: import java.util.*;
public class abc
{
Scanner keyboard = new Scanner(System.in);
private int num;
public void number()
{
num = keyboard.nextInt();
}
public int cda(int num)
{
this.num = num;
return num;
}
}It seems pretty straightforward to me... |
|
|
|
|
|
#2 |
|
Expert Programmer
|
You have to initialize each index of the array separately.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
WHat you have done is created an array which is ready to store abc class objects. However, you need to assign objects to it before you can try calling functions (initialize each index as titaniumdecoy said). Storing objects in arrays does work the same as other things, strings for example. You would need to assign a string to each index before you called string methods, its the same idea.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2006
Posts: 21
Rep Power: 0
![]() |
lol, yeah I see now... I was on it for like an hour, I kept on going over and over it, I really didn't see anything wrong with it, except the most obvious thing...
but what if I initialise values within the class? for instance if num = 1, wouldn't that mean its already initialised? Just wondering... oh and how do I intialize values for the array? |
|
|
|
|
|
#5 |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
You havent created an abc class object yet, so you would be unable to set num = 1, lol.
This is what you need to do. abc obj1 = new abc(); abc obj2 = new abc(); link[0] = obj1; link[1] = obj2; |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2006
Posts: 21
Rep Power: 0
![]() |
ok, I see.
but it does look like it will take a while, I mean I have to create a different object for every index in the array then I have to store them each in the array. Couldn't I just skip the first part? I mean, isn't an array basically a collection of similar objects? |
|
|
|
|
|
#7 |
|
Newbie
Join Date: May 2006
Posts: 28
Rep Power: 0
![]() |
no, an array is a place to store similar objects, you have to create them first.
|
|
|
|
|
|
#8 | |
|
Newbie
Join Date: May 2006
Posts: 6
Rep Power: 0
![]() |
Quote:
final int ARRAY_LENGTH = 10;
abc[] array = new abc[ARRAY_LENGTH];
for (int arrayPos = 0; arrayPos < ARRAY_LENGTH; arrayPos++)
array[arrayPos] = new abc();That code will fill the array with initalized abc objects. If you ever need to call a method in the 4th array position, for instance, you would then do: array[4-1].someMethod(); I'm not positive if it's conventional to do it that way, but I do it all the time anyway. :p |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|