9 Replies - 547 Views - Last Post: 15 April 2012 - 08:05 PM Rate Topic: -----

#1 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

One object in arraylist having more values than other

Posted 15 April 2012 - 06:24 PM

I have an arraylist of objects, there are two different objects that i want it to hold. One object would have values like: Name, Size, Solar System while another would have Name, Size, Solar System and Population. Is there any way to do this so that if i output the entire arraylist that it wont need to show a Population value for the first type of object?

So for example it might output something like this:

Mars, 15, Sol (for the first object type)
Earth, 20, Sol, 6000000000 (for the second object type)

Is This A Good Question/Topic? 0
  • +

Replies To: One object in arraylist having more values than other

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9082
  • View blog
  • Posts: 34,127
  • Joined: 12-June 08

Re: One object in arraylist having more values than other

Posted 15 April 2012 - 06:26 PM

Depending on how your objects are yea - just cast your array point as that object type and output the properties you need.
Was This Post Helpful? 0
  • +
  • -

#3 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: One object in arraylist having more values than other

Posted 15 April 2012 - 06:29 PM

Yeah but to do that wouldnt it need to know which type of object is at each index so it would know whether to include that value or not?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9082
  • View blog
  • Posts: 34,127
  • Joined: 12-June 08

Re: One object in arraylist having more values than other

Posted 15 April 2012 - 06:43 PM

I was understanding this as you wanted to collapse two objects into one... as in have one object with all four values but still treat it like the object with three values.
Was This Post Helpful? 0
  • +
  • -

#5 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: One object in arraylist having more values than other

Posted 15 April 2012 - 06:58 PM

No no i wanted it to output all the objects in the array without needing to include all the values in every object. Like one object has 3 values and one has 4, this is how my array is set up right now.

 id = (accounts.size() + 1);
            Random rand = new Random();
            int randBalance = rand.nextInt(10000);
            type = "Savings"; 
            accounts.add(new Accounts(id, type, randBalance));
            
            
            for (Accounts item : accounts) {
                System.out.println("Account Number: #" + item.accountId + "\t" + "Account Type: " + item.accountType + "\t" + "Balance = " + item.balance);
            }


Now there is another one with the same values except that it's "type" is Airmiles and it has an additional value. I dont want to include the additional value in the code i showed there, i dont want the other value to show up when i output the arraylist. That value should only show up when an object in the arraylist is of the right type.

So basically if the "type" value of the object is Savings then it doesnt show the extra value, but if the "type" value is Airmiles then it does show the extra value in the output.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9082
  • View blog
  • Posts: 34,127
  • Joined: 12-June 08

Re: One object in arraylist having more values than other

Posted 15 April 2012 - 07:07 PM

That just seems like.. well.. poor programming.

What you would want to do is send the object into a method that determines the type and returns a string value of what you wanted from that type... that returned value is what gets printed.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7644
  • View blog
  • Posts: 12,890
  • Joined: 19-March 11

Re: One object in arraylist having more values than other

Posted 15 April 2012 - 07:26 PM

Quote

That just seems like.. well.. poor programming.


I would have said poor design, but yes, six of one, 0110 of the other.

In your planets example, you have a number of planets, some of which are populated. So fine, you have a zero population on some planets. And in your display, you can check to see whether the planet is populated when you're printing, and display accordingly.

Your way, you're doing extra work, twice, and all you're gaining from it is extra maintenance. Extra work the first time to design a Populated Planet and an Unpopulated Planet (or whatever distinction you're making) and to get them to play nicely together, and then extra work again when you read in the Times that they've built a city on Mars, and now it's got a non-zero population. And the maintenance situation: you have to account for the needless difference between the two types every time you work with the classes. That's a bit of friction, a bit of sand in the gears, that makes your program more annoying and less fun to work on.
Was This Post Helpful? 0
  • +
  • -

#8 robgw3  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 138
  • Joined: 23-February 12

Re: One object in arraylist having more values than other

Posted 15 April 2012 - 07:29 PM

The only other way for me to do it would be to make two seperate arrays and thats not going to work as i have to output them to the same place. So i have to put them into one array and i have to have some way of saving one extra piece of data to some indexes of the array and not to others.

To put it simply, if i click button One it stores 3 values to one index of the array and if i click button Two then it stores 4 values to one index.

And no Jon they will never change, so the objects with 3 values will never have 4 values. That remains constant. The only way the index of the array will change from 3 to 4 or 4 to 3 is if one index is erased. I simply cant just leave a blank value in the object that only has 3 values.

This post has been edited by robgw3: 15 April 2012 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: One object in arraylist having more values than other

Posted 15 April 2012 - 08:01 PM

You can have an ArrayList<A>
A containing 3 of your variable
And a class B that extends A with the fourth value

When you remove object from the ArrayList you check if they are of the A or B type and react accordingly
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7644
  • View blog
  • Posts: 12,890
  • Joined: 19-March 11

Re: One object in arraylist having more values than other

Posted 15 April 2012 - 08:05 PM

In your example, it can change. Pick a better example, maybe.

Okay, so this is basically an inheritance question.
To put two objects of the same type into one arraylist, they have to have a type in common. In order to be any use at all, they should have a close parent (not Object, for example) and as many methods as possible.

Let's go back to everyone's favorite, the animals hierarchy.

Suppose you have Cat and Dog which are Mammals and implement HousePet. You could put Cats and Dogs in and ArrayList<Mammal> or in an ArrayList<HousePet>. Objects coming out of that ArrayList would know only of the methods appropriate for Mammal or HousePet. No object would know about methods of Cat or Dog, and if the list were typed as <Mammal> they wouldn't know about HousePet's methods.

So you make two classes in an inheritance hierarchy and you put them in a list by their nearest common ancestor or by some appropriate Interface that they implement, and there you go.

Both will have methods to output their data, and each will do that correctly, as you define it.



           for (Accounts item : accounts) {
               System.out.println("Account Number: #" + item.accountId + "\t" + "Account Type: " + item.accountType + "\t" + "Balance = " + item.balance);
           }



This is incorrect. Do this:

           for (Accounts item : accounts) {
               System.out.println(item);
           }



and let toString handle it.

View Postpbl, on 15 April 2012 - 10:01 PM, said:

You can have an ArrayList<A>
A containing 3 of your variable
And a class B that extends A with the fourth value

When you remove object from the ArrayList you check if they are of the A or B type and react accordingly



What am I reading? No, never check types. Objects need to know how to wipe their own bottoms. You don't need to be helping them with that.

This post has been edited by jon.kiparsky: 15 April 2012 - 08:06 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1