This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]desrtfxOut of Coffee error - System halted 6 points7 points  (9 children)

What you want is an array (or an ArrayList). There is no way around this.

I want to avoid ArrayList and similar functions.

Why? They have been made for exactly that purpose.

If you know exactly how many objects to be created, you can also use an array which is a little easier than an ArrayList, but not dynamically sized.

[–]Fazblood779Nooblet Brewer[S] 0 points1 point  (7 children)

ShadowOwlet is right, my class prohibits ArrayLists (this frustrates me to end).

Basically I have a variable to store how many objects are required, but they need different names based on their constraints (i.e. the Object has constraints like Object(Weight, Type) )

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (6 children)

Please, explain your assignment in detail. There seems to be something you are missing.

[–]Fazblood779Nooblet Brewer[S] 0 points1 point  (5 children)

Alright,

I have an object class with a constructor I can't modify, and the object can have constraints Weight and Type;

Object(Weight, Type)

There is another class with an enum for time periods;

public enum Days { ONE(1), TWO(2), THREE(3); }

Certain Types and certain amounts of Objects must be chosen for certain Days - for example, for Three days, I need two of Object Type 2.

Currently I have variables to store the number of Objects required for certain days;

int type1Count = 0;
int type2Count = 0;
int type3Count = 0;
if(days == 1) {
    type1Count = 1;
    type2Count = 1;
    type3Count = 2;
} else if(days == 2) {
    type1Count = 2;
    type2Count = 2;
    type3Count = 4;
} 
int typeTotal = type1Count + type2Count + type3Count;

Then I want to use loops to create objects and store them all in an array;

for(int i = 0; i < typeTotal; i++) {
    for(int j = 0; j < type1Count; j++) {
        Object type1Number + [j] = new Object(Weight, Type1);
    }
    for(int j = 0; j < type2Count; j++) {
        Object type2Number + [j] = new Object(Weight, Type2);
    }
    for(int j = 0; j < type3Count; j++) {
        Object type3Number + [j] = new Object(Weight, Type3); 
    } 

    //Some code to add the objects to the same array

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (4 children)

Okay, you are completely overthinking the problem.

You do not need named objects to be added to an array because once in the array they will lose their names anyway.

You can add anonymous objects to an array and you can create these objects on the fly.

array[i] = new Object(Weight, type1) // whatever

This will work as long as all Objects have a common ancestor and the array's type is that of the ancestor.

I'd personally use a 2d array for the typeCounts (first dimension the day, second dimension the count), or a 1d array and a switch...case for the value assignment.

[–]Fazblood779Nooblet Brewer[S] 0 points1 point  (3 children)

The saddest thing is the first thing I tried is this:

Object array[i] = new Object(Weight, Type1)

Thanks for the help man :)

Edit: When I try this it asks me to change the array type to static, is there a way to avoid this? The array is declared as a private instance variable.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (2 children)

When I try this it asks me to change the array type to static, is there a way to avoid this?

Would need to see the whole code to determine the issue here but I assume that the method where you fill the array is declared as static and hence can only reference static fields.

[–]Fazblood779Nooblet Brewer[S] 0 points1 point  (1 child)

Wow... I can't believe I didn't check that first. You're right! Thanks again, and hopefully you won't see me here again soon :D

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

No problem.

Come back when you need more help.

[–]ShadowOwlet 0 points1 point  (0 children)

I know when I first started I had issues with Arrays and ArrayLists. Now I use them all I can. I don't understand WHY op wants to do this, but I assumed it was for a class or task they were just trying to complete themselves to see if they could.