• 01-16-2014, 12:19 PM
Stud1
I'm studying about arrays and I have some questions:

First, is there any difference between these two?
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
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?
• 01-16-2014, 12:23 PM
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.
• 01-16-2014, 12:25 PM
gimbal2
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!
• 01-16-2014, 01:22 PM
Stud1

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:

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?
• 01-16-2014, 01:41 PM
gimbal2
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.
• 01-16-2014, 04:25 PM