• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Add method for a Bag array[]

 
Greenhorn
Posts: 13
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I'm implementing a java Bag using normal arrays.
So I'm writing a generic class that implements a generic interface.

This class contains an add method that is supposed to add elements to T[] bag. Since the array size is fixed, I'm not sure how to pull it off.


.
.


 
Ranch Hand
Posts: 180
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can always use a count variable to keep track of how many elements are there in the array.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are doing is trying to create an array which will hold all Ts put into it. I suggest you do what ArrayList does: when the array is full replace it with a larger array, until it is as large as the limits of the int datatype will permit. This thread will show you where you can find out more about ArrayList.

Why do you need a Bag class? Why can't you use one of the standard Collections Framework classes? Can't you write a wrapper round ArrayList<T>?
 
Sina Meraji
Greenhorn
Posts: 13
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What you are doing is trying to create an array which will hold all Ts put into it. I suggest you do what ArrayList does: when the array is full replace it with a larger array, until it is as large as the limits of the int datatype will permit. This thread will show you where you can find out more about ArrayList.

Why do you need a Bag class? Why can't you use one of the standard Collections Framework classes? Can't you write a wrapper round ArrayList<T>?



Thank you. I'm familiar with ArrayList. We've learned about bags at university and I'm trying to solve some problems related to it. In one of them I faced this issue (actually having to add elements to an array)
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you have to use an array? Is it part of the assignment? If so, you need to let us know that.
 
Sina Meraji
Greenhorn
Posts: 13
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Why do you have to use an array? Is it part of the assignment? If so, you need to let us know that.


It's not an assignment but yes it's part of the question and I was being curious. I'm getting ready for my final exam and I'm checking out some of the things I didn't check out before.
thanks
 
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic