Results 1 to 20 of 20
  1. #1
    ggalan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default array containing int strings

    when i have an array containing only int's i use
    Java Code:
    int arr[] = {43,56,76,8,65,76,2,31};
    what if my array contains a mixture of elements like strings and arrays, how would i do this?

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

    Default

    An array can only contain the type of element in it's declaration. You can make an array of strings and convert your integer values into strings.

    Example
    int arr[] = {43, 21, 11, 0, 17, 3};
    String[] strArray= new String [6];
    for (int i=0; i< arr.length; i++){
    strArray[i] = Integer.toString(arr[i]);
    }

  3. #3
    ggalan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    thank you. btw is there a difference where the brackets are placed?
    Java Code:
    String strArray[]= new String [6];
    String[] strArray= new String [6];

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

    Default

    Quote Originally Posted by ggalan View Post
    thank you. btw is there a difference where the brackets are placed?
    Java Code:
    String strArray[]= new String [6];
    String[] strArray= new String [6];
    There most certainly is. It needs to be placed after the type of array you're making, example:
    int [] xxxx ={0};
    or
    char[] xxxxxx = {'0'};

    Otherwise you're telling it that you're making an array of the variable name, which is something you won't be getting into for a while yet.

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

    Default

    Quote Originally Posted by ggalan View Post
    when i have an array containing only int's i use
    Java Code:
    int arr[] = {43,56,76,8,65,76,2,31};
    what if my array contains a mixture of elements like strings and arrays, how would i do this?
    You could create an array of Object and fill it with Strings and with Integers, the object wrapper for ints, but I strongly urge you not to do this as there's rarely a need to do this and it often makes for a very fragile program design. Better to create an class that will hold your different data types and then create an array or collection of objects of this class.

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

    Default

    Everything I have ever read says different.

    Java Code:
    int[] arr = new int[5];
    int arr[] = new int[5];
    Are the same, you can test it by creating both and filling them.

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

    Default

    Quote Originally Posted by ggalan View Post
    thank you. btw is there a difference where the brackets are placed?
    Java Code:
    String strArray[]= new String [6];
    String[] strArray= new String [6];
    These are two ways of declaring the very same thing. Please ignore sarevok's well meaning but misguided information.

    edit: as sunde has already stated above. Once again I'm too slow!

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

    Default

    Quote Originally Posted by Fubarable View Post
    These are two ways of declaring the very same thing. Please ignore sarevok's well meaning but misguided information.
    Hrm, odd, I'm just going by the way it was outlined in the arrays chapter in my Java book. Apologies about that.

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

    Default

    Quote Originally Posted by sarevok9 View Post
    Hrm, odd, I'm just going by the way it was outlined in the arrays chapter in my Java book. Apologies about that.
    Double check your book please as this is important to know.


    Also the information can be found in the Oracle Java tutorials array page, about half way down: http://download.oracle.com/javase/tu...ts/arrays.html


    You can also place the square brackets after the array's name:

    Java Code:
    float anArrayOfFloats[]; // this form is discouraged
    Last edited by Fubarable; 04-21-2011 at 10:39 PM.

  10. #10
    ggalan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    so they are the same?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    sunde887++

    To my eye at least, "int[] arr" is more readable: it says straight up that we are dealing with a (1-d) array of int. It is the more common form and the one that Eclipse uses by default when you autogenerate a main() method. But there is absolutely no difference, "int arr[]" is semantically identical and the concept of "an array of the variable name" (whatever that means) is not expressable in Java.

    ------

    As a weird hangover from C, or for some other reason, the square brackets can also go after a method name. As in

    Java Code:
    int foo()[] {
        return null;
    }

    Again, rather unreadable and even rarer "in the wild".

    [edit] very slow ;(

  12. #12
    ggalan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    interesting.
    ive been using actionscript and some javascript and used to
    Java Code:
    int arr[] = {};
    declaring the variable type then stating the arrays values

    but if this seems more java correct then i will adjust
    Java Code:
    int[] arr = {};

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

    Default

    Think of it this way, the bracket stands for array so which sounds better
    Java Code:
     
    Type array named name
    int[] varName
    
    Or 
    
    Type named name that is an array
    int varName[]

  14. #14
    ggalan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    makes sense, thanks!
    :)

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

    Default

    You are welcome. If you are satisfied, please mark your thread solved withthe thread tools at the top.

  16. #16
    ggalan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    1 last question, when you have a multidimensional array would the same apply?
    Java Code:
     int[][] arr = {{8,9,10,11},{12,13,14,15}};
    or
    Java Code:
     int arr[][] = {{8,9,10,11},{12,13,14,15}};

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

    Default

    Quote Originally Posted by ggalan View Post
    1 last question, when you have a multidimensional array would the same apply?
    Java Code:
     int[][] arr = {{8,9,10,11},{12,13,14,15}};
    or
    Java Code:
     int arr[][] = {{8,9,10,11},{12,13,14,15}};

    Not sure what you're asking so I'll try to answer two possible questions:

    Would both versions compile and run the same? The best and quickest way to find out is to test it in a small program.

    Are the same design and style issues present? Yes.

  18. #18
    ggalan is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    in terms of where the brackets are placed, in multidimensional arrays does it look more "proper" when brackets be placed right after the variable type as in
    Java Code:
    int[][] arr = {{8,9,10,11},{12,13,14,15}};
    as apposed to placing it after the variable name

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

    Default

    Quote Originally Posted by ggalan View Post
    in terms of where the brackets are placed, in multidimensional arrays does it look more "proper" when brackets be placed right after the variable type as in
    Java Code:
    int[][] arr = {{8,9,10,11},{12,13,14,15}};
    as apposed to placing it after the variable name
    Yes, the same design and style considerations apply for multi-dimensional arrays.

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

    Default

    Also, if you do work with multi dimensional arrays think of it this way. It's a 1 d array where each element is another array.

Similar Threads

  1. extract strings from an array
    By plasticfood in forum New To Java
    Replies: 18
    Last Post: 10-02-2010, 01:15 PM
  2. Strings as array help please..
    By crazygurl in forum New To Java
    Replies: 1
    Last Post: 12-01-2009, 08:23 PM
  3. 2 dimensional array with strings.
    By dbashby in forum New To Java
    Replies: 12
    Last Post: 10-13-2009, 10:52 PM
  4. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 07:32 AM
  5. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 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
  •