## sk8ergirl

hi,
how can I make the size of the array is equal to the number of elements in the array

## JamesCherrill 3,661

You can't. An array size is fixed when it is created, and cannot be changed.
All you can do is to create a second array of the right size, copy all the entries into that one, point any reference variables to the new array, then allow the original array to be garbage collected.
Or use an ArrayList.

## Stuugie 50

Hi, that's sort of a vague question. If you you have elements in the array then it already has a size. You can find the size of the array by using its length property. For instance:

``````int[] ary = new int[10];
int j=0;
for (int i=0;i<ary.length;i++)
{
j++;
ary[i]= j;
}
``````

## JamesCherrill 3,661

I think sk8ergirl is referring to the problem where you have a (large) array that's not fully populated with actual data, so you want to "fit" the array to the number of elements that you have actually used.

## Stuugie 50

Thanks JC, so would this be helpful:

``````public static void main(String[] args) {

//Create an ArrayList of Integer type
ArrayList<Integer> ary = new ArrayList<Integer>(10);
//Add only 6 values to the array
//Create another ArrayList to hold only the values that are
//in the previous array, and not the whole size of the array
ArrayList<Integer> ary2 = new ArrayList<Integer>(ary.size());
System.out.println(ary.size());
//Load the ary values into the ary2 array.
System.out.println(ary2.size());
}
``````

## JamesCherrill 3,661

Like that, but with arrays. There's no point doing that with ArrayLists.

commented: Thanks for helping with my helping confusion +2

## Stuugie 50

Sorry, I was just trying stuff that I thought may help. I can't see how this can be done without using an ArrayList but I'm not the greatest with working with Java.

## Stuugie 50

Is this closer?

``````public static void main(String[] args) {

int cnt = 0;
Integer[] arry;
arry = new Integer[]{2,4,6,7,null,null,null,null};
for (int i = 0;i<arry.length;i++){
if (arry[i]!=null){
cnt++;
}
}
Integer[] arry2 = new Integer[cnt];
for (int i = 0;i<arry2.length;i++){
arry2[i]=arry[i];
System.out.println(arry2[i]);
}
}
``````

LoL, I feel like I should be asking the same question as sk8tergirl.

## IIM 163

no....it will just take more memory as you are string null in array.
Better go with arraylist and if you seriously want array as outout,then change aray to arrayList using toArray() method of List.MAke array of size of ArrayList.

## Stuugie 50

it will just take more memory

Is that really an issues nowadays?

## sk8ergirl

thank you Stuugie I think I'm closer now to solve the problem ^^

## sk8ergirl

@IIM I wanted to do it with arrayList , but I was asked to do it using array , arrayList is easier to use for this kind of problem

## Stuugie 50

LoL, I'm learning too but I'm glad I could help a bit.

## JamesCherrill 3,661

Now you know what needs to be done, you may want to look at the Arrays.copyOf method, which implements just what we were talking about.