Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Questions about arrays

  1. #1
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default 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. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,072
    Rep Power
    6

    Default 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!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default 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 12:32 PM.

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,072
    Rep Power
    6

    Default 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.
    masijade likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Questions about arrays

    Yes, an ArrayList is what you actually want.

  7. #7
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default 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

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-02-2012, 11:17 PM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  5. A few questions about arrays and loops
    By Jamison5213 in forum New To Java
    Replies: 1
    Last Post: 12-22-2009, 05:59 AM

Posting Permissions

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