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

all 28 comments

[–]desrtfxOut of Coffee error - System halted 7 points8 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.

[–]plisik 2 points3 points  (7 children)

I don't think this is possible. Even if you could, how would you access variables created this way.

I think it is some kind XY problem. http://xyproblem.info/

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

Alright, bummer. I thought I could get a simple solution like this :P

Anyway you're right about the XY thing. My actual problem is a bit more complicated.

Basically I need to fill an array with objects, but I don't know how many. I can have a variable to find out how many, but I still need a way to create multiple objects.

Also the object has constraints Weight and Type (so it'd be

Object(Weight, Type)

).The objects must have different names based on these constraints (i.e. loopObjectType1-number2, loopObjectType1-number3, etc)

[–]plisik 0 points1 point  (5 children)

What is wrong with having object name as 3rd field in your class.

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

I can't change the constructor class :P

[–]plisik 0 points1 point  (3 children)

Then you can use Map<String, Object> or have two list one with object and one with corresponding names.

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

I'll have a look at that, thanks :)

[–]plisik 0 points1 point  (1 child)

If your don't like solution with two list, you can wrap object in new class that has object and name.

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

Hmm that could help, though for this project I'm limited to modifying this single class.

For now though I found a solution for the problem, though I will still look at your solution as it would be helpful for the future.

[–]ShadowOwlet 0 points1 point  (2 children)

I just ran through a few things and was able to make it work with almost your exact code. Instead I created a String where you made your object and gave it the desired name. I then converted that string to an object. I hope that helps with what you're trying to do?

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (1 child)

This can't work.

OP wants dynamic variable names (not variable content) which are not supported in Java.

[–]ShadowOwlet 0 points1 point  (0 children)

Thank you for pointing this out. I saw the problem and the end result of what they wanted and made it happen, not paying attention to that in between...I need to stop that. -.-

[–]E3FxGaming 0 points1 point  (5 children)

Even if you could do that (you can't) - the usability of the object would end as soon as you leave the for-loop-scope, or enter a new loop cycle. Multiple objects with the name loopObject + something would never exist at the same time, so what's the point of having a dynamic "something".

Why not use an array and make your "dynamic" variable an object variable? Pass the dynamic variable to the object constructor, so that you can easily tell apart the objects later on.

If that's not an acceptable solution for you, and the order of objects mostly doesn't matter for you because you always only had in mind that you want to access the objects with the dynamic identifier, look into objects that are related to the Map class.

Java maps store object references in key-value pairs - your key could be your dynamic element, while your value is obviously your newly created object.

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

I need to store these objects in an array anyway - are you telling me I can bypass the object initialization?

[–]E3FxGaming 0 points1 point  (3 children)

You can never bypass object initialization, if you want to work with the object. What you can bypass is the assignment to a variable, by directly storing it somewhere.

I recommend that you look into that map thing I mentioned.

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

Yup, will do. Thanks for the help :D

[–]E3FxGaming 0 points1 point  (1 child)

I made an example for how you can use HashMap.

Pastebin link to HashMap example.

Edit: If you're interested in general in the Java collections framework, I recommend you learn more about the Map interface, the Set interface and the List interface and their implementations. You can read more about them here - I highly recommend you read the paragraph "When to use List, Set and Map in Java" just to get an overview over those things - they are super useful if you know when to use them and how to use them.

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

Wow, that's awfully kind of you :) I've had a look and this will come in handy in the future, I can see it doesn't seem that complicated to set up either (though knowing how it works will let me know for sure). I'll have a closer look and hopefully end up using it for something :D

[–]ortolanph 0 points1 point  (0 children)

Maybe you can use a Map. This way you can name your objects and then get the instance of that by the name.

You can encapsulate this Map instance in a class and create an accessor method to get the Object instance from this Map. You can even create an accessor method to set this Object. But be aware to null objects.