my dog learned polymorphism
The moose likes Beginning Java and the fly likes array size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array size" Watch "array size" New topic

array size

lauren kris

Joined: Jul 01, 2008
Posts: 8
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

Joined: Jun 09, 2008
Posts: 5
This might be helpful.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15087

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Saurabh Patil
Ranch Hand

Joined: Dec 11, 2005
Posts: 47

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.

Preparing for OCPJWCD 6.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
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

Joined: Oct 02, 2003
Posts: 11955

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?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 692
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:
subject: array size
It's not a secret anymore!