File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array size

 
lauren kris
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused... I have to create an array and limit the size between 2 and 10. I am not sure how to do this. Any suggestions?
 
Joseph Fisher
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be helpful.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Arrays in Java have a fixed size, which is determined when you declare the array. For example:

String[] names = new String[10];

This creates an array of 10 strings. If you want a list with a variable number of elements, you should use one of the classes from the collections API, for example an ArrayList:

However, there is no easy way to make it so that the ArrayList can only contain between 2 and 10 elements.
 
Saurabh Patil
Ranch Hand
Posts: 47
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The array index out of bound exception only checks for the upper limit of your array, so you can check the upper limit of 10 easily. But I dont know if there is something in java which enables us to check the lower limit as well. But if you yourself want to check it, then may be you can write your own exception by extending Exception class.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Saurabh Patil:
. . . extending Exception class.
Good idea. Maybe quicker to throw an IllegalArgumentException if the size of the array is outside the requisite range. Use myArray.length in any for loops; that way you can make sure not to exceed the actual size of the array and get an ArrayIndexOutOfBoundsException.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you also write a wrapper that has an array with the 9 elements you need, and use your getters/setters to enforce the 2-10 inputs?
 
Norm Radder
Ranch Hand
Posts: 707
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the problem as only allowing arrays with length = 2,3, .. up to 10.
Indexing the array is as usual. Ie 0 to arraysize-1.

Sounds like a new class is needed with a constructor that checks that the size of the array is in limits, otherwise it throws an exception.

Since the OP didn't say what the array can hold, can it hold whatever the OP wants it to hold?

On further thought, arrays are defined by the language, how do you change that? You can define a class that is sort of like an array, but it is still not an array.

A solution is a method that returns an array with the desired limits:


How much of this can be done with Generics?
[ July 02, 2008: Message edited by: Norm Radder ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic