Results 1 to 9 of 9

Thread: size of array

  1. #1
    Join Date
    Sep 2010
    Posts
    18
    Rep Power
    0

  2. #2
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Assuming that you have an array declared, you can use the length parameter to discern it's length.

    If you are asking how you set the size of an array, that is totally up to the user.

    Example:
    int[] array1 = new int[10];// Makes a new character array with a length of 10, which will be 0-9 since arrays are 0-indexed.
    for (int i=0; i < array1.length; i++){
    System.out.println("Hello World");
    }

  3. #3
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    or you could iterate over the array in this way:
    Java Code:
    for(type t : arrayName){
        System.out.println(t);
    }
    Type would be what ever the type array you have. String Array would be like so:
    Java Code:
    for(String t : arrayName){
        System.out.println(t);
    }
    So the Sysout would print the place of arrayName[0-...];
    Last edited by rizowski; 04-21-2011 at 09:06 PM.

  4. #4
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default

    sarevok9 answers this question though i would like to draw your attention to arraylists that increase and decrease in size automatically as elements are added to them; note the below example:
    ArrayList <Integer> name = new ArrayList<Integer>();
    to add to an arraylist: name.add(index,added value) or name.add(added value)
    arrays lists can be created for strings and any other type of primitive using the wrapper classes of the primitive types (for example Integer instead of int for arrays)
    to find the size of an arraylist: name.size();
    make sure to include at the beginning of the code-import.java.util.*;-

  5. #5
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Not my topic, but I would like to as you a question Farahm-

    Why are you putting the 'Integer' in <'s and >'s? Does this have something to do with the constructor? I left my java book at home and I'm in my college right now.

  6. #6
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default

    Yes it is required while declaring an arraylist.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by farahm View Post
    Yes it is required while declaring an arraylist.
    Generic declarations are not required, and if they were they'd break Java's backwards compatibility, but they are encouraged in order to allow for compile-time type checking.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    To elaborate on what they said, the generics specifies the type the arraylist(or other collection) contains. If you say Integer it let's you add integers to the array list.

    In an array you do
    Java Code:
     
    Type[] varName
    
    In a collection you do
    
    Collection<Type> varName
    If you dont specify a type it fills it with objects. The generic parameters make it so the object is cast to the correct type. Check out the oracle generics tutorials for more thorough explanations.

  9. #9
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Generic declarations are not required, but they do allow the compiler to check your code more thoroughly, and if you don't use them, you'll get a warning - which some find disturbing ;)

Similar Threads

  1. Changing size of Array
    By ravian in forum New To Java
    Replies: 3
    Last Post: 06-05-2012, 09:17 PM
  2. increase array size
    By giorgi in forum New To Java
    Replies: 45
    Last Post: 04-08-2011, 05:56 PM
  3. Doubling the size of an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:42 PM
  4. Array size declaration
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 01-18-2008, 11:37 PM
  5. Maximum size of an array
    By Hasan in forum New To Java
    Replies: 1
    Last Post: 05-20-2007, 12:11 PM

Posting Permissions

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