Results 1 to 5 of 5

Thread: Bounded Array

  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Bounded Array

    We can declare an ArrayList bounded by String as shown below:

    Java Code:
    ArrayList<String> arrayList = new ArrayList<String>();
    Can we also declare a simple array bounded by String or some other datatype? Please write syntax.

    Thanks in advance.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Smile

    Hello bugger.

    I think it's just the normal declaration of an array of type String:

    Java Code:
    int size = 10;
    String[] array = new String[size];
    or in Synax:

    Java Code:
    int size = 10;
    <type here>[] array = new <type here>[size];
    Hope this helps. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    If you are concerned on including the Generics things as well, make it like

    Java Code:
    int size = 10 ;
    ArrayList<Object> array = new ArrayList<Object>[size] ;
    If your elements in array are not of same type then you need to cast them to their original type before using them. instanceof operator can be useful in this case.
    dont worry newbie, we got you covered.

  4. #4
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Whenever i want to have a group of objects of a type other than String or ints or doubles, i use ArrayList. It's better in the sense that it doesn't have a fixed length. When you have an array there can only be a certain number of objects in that array, but in an ArrayList you can add as many as you'd like.

    I also like using HashSet when order doesn't matter, and when you want it to not repeat elements. A while ago i started on my first real test of my logic skills: a Sudoku game, and, my knowledge didn't really offer a solution to how i could check if the user won. But, when i learned about HashSets it completely solved my problem. Basically what i did was, add all the numbers (of a row, column, or square) the user entered into the HashSet, and if the size of the HashSet is less than 9, they didn't win (there's a method to check the rows, colums, and squares). The reason for this is, if there are not 9 number in the HashSet, that means that the user repeated a number (and HashSets silently ignore when a duplicate element is an argument of the add method), which means the user didn't win the game.

    So yeah i mean i guess everyone has their own style. Different people like using different classes that do virtually the same thing. Its just a matter of preference.

  5. #5
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks all of you. It was a simple thing that I was not thinking of:

    Java Code:
    int size = 10;
    String[] array = new String[size];
    But I learnt that if size is not defined, we can use ArrayList. And also generics can be used.

    Again, thanks guys.

Similar Threads

  1. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 03:25 AM
  2. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 03:34 PM
  3. 2D array
    By bluekswing in forum New To Java
    Replies: 2
    Last Post: 01-15-2008, 06:57 PM
  4. Help with Array
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:32 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
  •