# Thread: Questions about arrays

1. Member
Join Date
Jan 2014
Posts
44
Rep Power
0

## Questions about arrays

I'm studying about arrays and I have some questions:

First, is there any difference between these two?
Java Code:
```int x[] = new int[3];
int[] x = new int[3];```
It seems to me when I try them they do exactly the same, is that correct?

Second, more important question. If I want to make an int variable that refers to the index number of an array, how do I write?

For example if we have
Java Code:
`String[] string = new String[10];`
and I want to have a variable "int n" that refers to an index number, so that if I set n = 5 then string[5] is selected. Note that the int n is NOT an array, but just a regular integer variable. How can I do that?

2. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11

## Re: Questions about arrays

To the first question, no, they are exactly the same. Standard conventions prefer the second example.

n = 5;
String s = string[n];

NOTE: arrays are zero based (the first index is 0 not 1) so string[5] is the SIXTH string in the array.

3. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Questions about arrays

Is there a difference: technically no (you can test that you know), but the second version is what you should use because everyone does that

The second question makes no sense. You don't select things in an array. What do you functionally want to achieve?

EDIT: ninja'd again!

4. Member
Join Date
Jan 2014
Posts
44
Rep Power
0

## Re: Questions about arrays

Thanks masijade, your reply was very helpful.

I have some other question. I want to remove one element from an array and then shorten the total array length by 1. How do I do this the simplest way?

I was thinking something like this:

Java Code:
```public void removeElement(int element) {
for (int i = element; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
array[array.length - 1] = null;
array.length--;
}```
However, the last line is an illegal operation. As you can see I want to make the length of the array -1. How can it be done?
Last edited by Stud1; 01-16-2014 at 01:32 PM.

5. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Questions about arrays

You can't, arrays are static.

Two options:

1. create a new array and do a copy
2. use an ArrayList in stead

I'd prefer #2.

6. Senior Member
Join Date
Jun 2008
Posts
2,571
Rep Power
11

## Re: Questions about arrays

Yes, an ArrayList is what you actually want.

7. Senior Member
Join Date
Jan 2013
Posts
168
Rep Power
4

## Re: Questions about arrays

Arrays are static, so their size will always be the same as you originally. If you use ArrayLists, which you can learn about here: ArrayList (Java Platform SE 7 )
you will be able to use the remove() method on the last value, which will remove it and shorted the ArrayList

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•