Author |
Message |
uberwalla
![](http://www.mac-help.com/images/starwars.gif)
|
Posted: Fri Feb 22, 2008 7:34 pm Post subject: How to store arrays in an array? kind of? |
|
|
Ok i am not trying to make a 2 dimentional array. just trying to be able to call on other arrays using an array.
basically what i want to do is have some arrays:
Thats basically what i want to do (call the arrays from that 'all' array. but i keep getting the errors:
Java: |
roman. java: 54: incompatible types
found : java. lang. String[]
required: java. lang. String
all [0] = ones;
^
roman. java: 55: incompatible types
found : java. lang. String[]
required: java. lang. String
all [1] = tens;
^
roman. java: 56: incompatible types
found : java. lang. String[]
required: java. lang. String
all [2] = hundreds;
^
roman. java: 57: incompatible types
found : java. lang. String[]
required: java. lang. String
all [3] = thousands;
^
4 errors
|
any idea how i can make this work? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Fri Feb 22, 2008 7:48 pm Post subject: RE:How to store arrays in an array? kind of? |
|
|
That doesn't make sense. You have 4 arrays of Strings which you are then treating as individual Strings when initializing the all array. Choose either to have ones, tens, hundreds, and thousands as Strings or else make the all array 2-dimensional.
You'll have to give more info on the problem if you're trying to avoid multi-dimensional arrays. What exactly do these Strings/arrays contain? At first glance it looks like you're over complicating the matter in the first place. |
|
|
|
|
![](images/spacer.gif) |
richcash
|
Posted: Fri Feb 22, 2008 7:56 pm Post subject: Re: How to store arrays in an array? kind of? |
|
|
[Gandalf] is correct, you are treating arrays of Strings as Strings. Why not do this?
code: | String [] [] all = new String [4] [10]; |
But you'll have to make your thousands array the same length as the others if you want to make an array of arrays, right? |
|
|
|
|
![](images/spacer.gif) |
uberwalla
![](http://www.mac-help.com/images/starwars.gif)
|
Posted: Fri Feb 22, 2008 8:00 pm Post subject: Re: How to store arrays in an array? kind of? |
|
|
these arrays are containing a bunch of roman numerals as such:
Java: |
String [] ones = new String [10];
ones [0] = "";
ones [1] = "I";
ones [2] = "II";
ones [3] = "III";
ones [4] = "IV";
ones [5] = "V";
ones [6] = "VI";
ones [7] = "VII";
ones [8] = "VIII";
ones [9] = "IX";
String [] tens = new String [10];
tens [0] = "";
tens [1] = "X";
tens [2] = "XX";
tens [3] = "XXX";
tens [4] = "XL";
tens [5] = "L";
tens [6] = "LX";
tens [7] = "LXX";
tens [8] = "LXXX";
tens [9] = "XC";
String [] hundreds = new String [10];
hundreds [0] = "";
hundreds [1] = "C";
hundreds [2] = "CC";
hundreds [3] = "CCC";
hundreds [4] = "CD";
hundreds [5] = "D";
hundreds [6] = "DC";
hundreds [7] = "DCC";
hundreds [8] = "DCCC";
hundreds [9] = "CM";
String [] thousands = new String [4];
thousands [0] = "";
thousands [1] = "M";
thousands [2] = "MM";
thousands [3] = "MMM";
|
i want the all array:
Java: |
String [] all = new String [4];
all [0] = ones;
all [1] = tens;
all [2] = hundreds;
all [3] = thousands;
|
to hold those other arrays so i can run something like:
Java: |
numstr = number.toString();
length = numstr.length;
romanstr = '';
for (i = 0; i < length; i++) {
romanstr += all[length - i - 1][numstr.charAt(i)];
}
|
I am also not sure if this is possible in java or not as i am trying to convert an old javascript program i have to plain java. (so yes i know some of those commands aren't actually pure java commands)
so basically is there a way i can recreate that into java? so that i can convert a integer a roman numeral (that's what i am making this program for)
If you need more info still. I can try to explain what I am trying to achieve better.
thanks,
uber~ |
|
|
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Fri Feb 22, 2008 8:48 pm Post subject: Re: How to store arrays in an array? kind of? |
|
|
code: | String[][] all = new String [4][]; |
|
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Feb 23, 2008 2:12 am Post subject: RE:How to store arrays in an array? kind of? |
|
|
Don't use arrays? Collections are your friend? |
|
|
|
|
![](images/spacer.gif) |
Aziz
![](http://compsci.ca/v3/uploads/user_avatars/17740604804829f8242e90c.png)
|
Posted: Sat Feb 23, 2008 12:51 pm Post subject: RE:How to store arrays in an array? kind of? |
|
|
Doing it with arrays:
When you declare an array as String[], you are saying that each element in the array is a String. But what you're trying to do is assign each element to an array.
Type[]
Type is the type of each element in the array
[] declares that it's an array. Together these declare an array type.
So to make and array of arrays (which is what a mulitdimensional array is in Java, by the way), you make the type an array. And an array type is Type[]. So it would be
Type[] []
or more commonly
Type[][].
What that syntatically means is "An array of arrays of Strings", or "an array whose elements are each an array. Each one of those arrays have elements that are String".
ANd like OneOffDrive said, in a not so expressive way, was that you can give the first index a length, but you don't have to specify the other length, as each array in the array can have a different length. Called jagged array in some languages where there are different array types (like C#) |
|
|
|
|
![](images/spacer.gif) |
uberwalla
![](http://www.mac-help.com/images/starwars.gif)
|
Posted: Sat Feb 23, 2008 1:53 pm Post subject: Re: How to store arrays in an array? kind of? |
|
|
i have actually solved the problem from earlier. thanks a lot though guys. seems i did need to declare all as [] [].
i thought it was just going to be a multidimentional array like in javascript which wouldn't have worked for what i was trying to do. but indeed it did work because i needed the second array parameter to call on the other arrays.
thanks to all those who helped. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Aziz
![](http://compsci.ca/v3/uploads/user_avatars/17740604804829f8242e90c.png)
|
Posted: Sat Feb 23, 2008 4:58 pm Post subject: RE:How to store arrays in an array? kind of? |
|
|
Good. Make sure you understand what you're doing, too and why it works. I hate to sound like wtd, but that's more important then getting it to work (especially in an academic environment) |
|
|
|
|
![](images/spacer.gif) |
uberwalla
![](http://www.mac-help.com/images/starwars.gif)
|
Posted: Sat Feb 23, 2008 5:03 pm Post subject: Re: How to store arrays in an array? kind of? |
|
|
yea i understand how it works. i needed the extra array in it to act as a means of calling the other arrays. because i was trying to call ones not ones [] |
|
|
|
|
![](images/spacer.gif) |
|