aspose file tools*
The moose likes Java in General and the fly likes dynamic array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "dynamic array" Watch "dynamic array" New topic
Author

dynamic array

Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1874
Hi,
I want to write a method which accepts variable number of elements.Method will do some work and return again variable number of elements.
I can use List/ArrayList can be used but element is of type integer.and there can be maximum of 10 numbers.Any other appraoch than using ArrayList?


MH
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi!

Perhaps you could create your own container class as a wrapper around a List/ArrayList. There you could implement for example an add() method which constrains inserting of elements to a maximum number of 10.
And for type safety you could use a generic ArrayList<Integer> instead of a plain list. Or you could check at runtime with "instanceOf Integer".

Marco
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
If you know the type of variable and a maximum size in advance, try an array with the same size as the maximum, and a count variable. Whenever you add an element add it at [count++] and whenever you remove one you will have to move all the subsequent elements 1 back and decrement count by 1.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Do you really need to *enforce* that there won't be more than 10 numbers?

Otherwise, why not just use an array?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
abu alfouz
Ranch Hand

Joined: Nov 20, 2006
Posts: 44
Why you don't use a list contains wrapper class "java.lang.Integer"?


SCJP 1.5
SCWCD 1.4
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: dynamic array