Results 1 to 3 of 3

Thread: quick q&a

  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default quick q&a

    I recently knew that instead of writing
    Java Code:
    int n = 0;
    i can write
    Java Code:
    int n;
    well what about arrays
    can i write
    Java Code:
    int [] array;
    instead of
    Java Code:
    int [] array = new [n];
    also, if yes, how can i use the array "array" later?
    i want to assign to it later n elements

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aizen92 View Post
    I recently knew that instead of writing
    Java Code:
    int n = 0;
    i can write
    Java Code:
    int n;
    well what about arrays
    can i write
    Java Code:
    int [] array;
    instead of
    Java Code:
    int [] array = new [n];
    also, if yes, how can i use the array "array" later?
    i want to assign to it later n elements
    Yep, you can do that: you just define 'array' to be an array of ints but you don't initialize it, so:

    Java Code:
    int[] array; // defined but not initialized;
    int[] array= new int[3]; // defined and initialized to three zero elements
    int[] array= { 1, 2, 3 }; // defined and initialized to elements 1, 2 and 3
    If you don't initialize your array you have to assign an array to it later if you want to use it:

    Java Code:
    array= new int[3]; // assign a new array of three zero elements
    array= new int[] { 1, 2, 3 }; // assign an array with elements 1, 2 and 3
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

Similar Threads

  1. Quick Help!!
    By MrA in forum New To Java
    Replies: 1
    Last Post: 11-10-2010, 07:38 PM
  2. Quick help
    By locazor in forum New To Java
    Replies: 15
    Last Post: 10-17-2010, 07:07 PM
  3. Quick help please :confused:
    By Eimo in forum Advanced Java
    Replies: 0
    Last Post: 10-08-2010, 06:48 AM
  4. Quick help
    By Qwertypoiuy in forum New To Java
    Replies: 5
    Last Post: 01-06-2010, 10:49 PM
  5. Need help quick!
    By Manikyr in forum New To Java
    Replies: 11
    Last Post: 01-28-2009, 04:08 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
  •