Results 1 to 20 of 20
Thread: array containing int strings
- 04-21-2011, 09:28 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
- 04-21-2011, 09:36 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
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]);
}
- 04-21-2011, 10:05 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
thank you. btw is there a difference where the brackets are placed?
Java Code:String strArray[]= new String [6]; String[] strArray= new String [6];
- 04-21-2011, 10:20 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
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.
-
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.
- 04-21-2011, 10:28 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Everything I have ever read says different.
Are the same, you can test it by creating both and filling them.Java Code:int[] arr = new int[5]; int arr[] = new int[5];
-
- 04-21-2011, 10:30 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 37
- Rep Power
- 0
-
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.
- 04-21-2011, 10:39 PM #10
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
so they are the same?
- 04-21-2011, 10:46 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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 ;(
- 04-21-2011, 11:51 PM #12
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
interesting.
ive been using actionscript and some javascript and used to
declaring the variable type then stating the arrays valuesJava Code:int arr[] = {};
but if this seems more java correct then i will adjust
Java Code:int[] arr = {};
- 04-21-2011, 11:56 PM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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[]
- 04-22-2011, 12:00 AM #14
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
makes sense, thanks!
:)
- 04-22-2011, 12:10 AM #15
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are welcome. If you are satisfied, please mark your thread solved withthe thread tools at the top.
- 04-22-2011, 03:41 PM #16
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
1 last question, when you have a multidimensional array would the same apply?
orJava Code:int[][] arr = {{8,9,10,11},{12,13,14,15}};
Java Code:int arr[][] = {{8,9,10,11},{12,13,14,15}};
-
- 04-22-2011, 03:49 PM #18
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
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
as apposed to placing it after the variable nameJava Code:int[][] arr = {{8,9,10,11},{12,13,14,15}};
-
- 04-22-2011, 04:40 PM #20
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
extract strings from an array
By plasticfood in forum New To JavaReplies: 18Last Post: 10-02-2010, 01:15 PM -
Strings as array help please..
By crazygurl in forum New To JavaReplies: 1Last Post: 12-01-2009, 08:23 PM -
2 dimensional array with strings.
By dbashby in forum New To JavaReplies: 12Last Post: 10-13-2009, 10:52 PM -
storing strings into an array
By anthonym2121 in forum New To JavaReplies: 2Last Post: 04-04-2009, 07:32 AM -
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:39 PM


LinkBack URL
About LinkBacks


Bookmarks