• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

multi dimension array

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

I am just getting started with Java and it's good fun.

I'd need to know a couple of things.
I hope someone here will be able to shed some light on the following questions for me


First: is it possible to have a multi dimension array using different types of variables?
let's say the first dimension would be int, the 2nd dimension would be a string...



2nd: in the code (see below) I am using a 2 dimensions array and I store some values into it like this:
multiDollar[i][0] = dollarEarned[i];
multiDollar[0][i] = dollarEarned2[i];

is it possible to do this using a single line?


3rd: apparently there is a problem in my loop as the out.println return only 2000

Really sorry if these questions are trivial but I have just started sinking my teeth in Java...
Thanks!

4th: in Eclipse is it possible like in Excel VBA to define 2 points in the code, execute the code and when the program reaches point 1 stops, you can then execute the rest manually and check the values in the variables. (I found the "watch" feature really useful in VBA to debug a few problems in my loops)


and now the code: (it doesnt do anything special, it is just for me to get up to speed with variables, classes, loops... in java)


import static java.lang.System.out;



Thanks!!


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

choubix alex wrote:First: is it possible to have a multi dimension array using different types of variables?
let's say the first dimension would be int, the 2nd dimension would be a string...


There is no such thing as a multi dimension array in Java. All there is are arrays of arrays of X (where X can be anything). Indexes are always ints, starting at 0. You may want a String[][] - an array of arrays of Strings. The outer array contains (references to) String[] objects. Each of these String[] objects contain (references to) String objects.

2nd: in the code (see below) I am using a 2 dimensions array and I store some values into it like this:
multiDollar[i][0] = dollarEarned[i];
multiDollar[0][i] = dollarEarned2[i];

is it possible to do this using a single line?


You can remove the enter and spaces after the first ; but it will still be two separate statements.

3rd: apparently there is a problem in my loop as the out.println return only 2000


Apart from multiDollar[0][0], you do not give multiDollar[i][i] any value. What you are actually doing with multiDollar is create this array:
That's because you only assign values to the sides; with either the row number 0 or the column number 0. As for row 0 and column 0, you first give it value dollarEarned[0] but then overwrite it with dollarEarned2[0] - which is 2000. What is it you actually want to do?
 
choubix alex
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
thanks for your detailed explanations!

I'd just to make sure I fully understand point 1 and 4.

point 1:
There is no multi dimension array but just arrays of arrays,
My arrays of arrays have to be of a single type (either int[][], String[][]...) ?

Am I right in saying that, if this is the case, I have to "cast" a String into another type of variable when using it later? (let's say I create an array of arrays were String[] is a String and the 2nd level ([][]) is actually an integer that I store in my array as a String. later I'd have to convert the string back into an integers again)

point 4:

actually I am trying to print this on screen:

Dollar earned : 1002000
Dollar earned : 2004000
Dollar earned : 3505500

where :
1002000 would be multiDollar(0,0)
2004000 : multidollar(1,1)
3505500: multidollar(2,2)

It seems that my loop is not very clear as you didnt understand what I was trying to do.
(not your fault at all, it's definitely mine!!)

later I'll want to write:

dollar earned: 2100
dollar earned: 4200
dollar earned: 5850

using the array of arrays.

As I said: this code doesnt "make sense". it is just a way for me to get used to Java and its conventions and basic features

I'll give another shot at my code tomorrow morning in the office. maybe it will be crystal clear after a good night of sleep! ;)

good night!!





 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you would like to store different datatypes in a single array. Use the array of type Object. This was you can store object references and can also store primitives like int, float, long, double, etc. as objects using their corresponding wrapper classes Integer, Float, etc.
 
choubix alex
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I have partially cornered the problem: I was a bit blur on how to use a 2D array properly in java.

for instance:

int[][] multiDollar = new int[1][dollarEarned2.length];

would mean that multiDollar has 2 rows and 3 columns (because dollarEarned2.length = 3).
and each row represents an array and each column represents the position within the array.


Still I have a problem understanding how to store correctly dollarEarned[] and DollarEarned2[] in multiDollar[][]
I've been trying using 2 "for" loops but with no success.

basically I am trying to get this (using loops to store):

multiDollar[0][0] = 100
multiDollar[0][1] = 200
multiDollar[0][2] = 350
multiDollar[1][0] = 2000
multiDollar[1][1] = 4000
multiDollar[1][2] = 5500


have someone a chunk of code lying around that would show how to store 2 arrays in a 2D array using doulbe nested loops please?
thanks!


 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

for instance:

int[][] multiDollar = new int[1][dollarEarned2.length];

would mean that multiDollar has 2 rows and 3 columns (because dollarEarned2.length = 3).



Actually it would have 1 'row' and three 'columns'.

Although as Rob said, it is actually declaring an array of one element, that element is an int[] that holds 3 elements.

To iterate through an arbitrary 2D array using nested `for` loops, you would do something like this:



Alternatively, if you really don't need to keep track of the indexes you could use nested foreach (enhanced for) loops
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a one-dimensional array can only hold one type of thing - Integers, Strings, Widgets...whatever. using polymorphism, you can declare your array to hold the super-class, and put in an appropriate sub-class. so you could declare a one-dimensional array of "Animals", then stick in Dogs, Cats, and Birds, but not Trees. When you pull them out, you have to be careful you don't try and call the fly() method on your Dogs, but that can be dealt with.

When you have a "two dimensional" array, i.e. "Widgets[][]", you really have an array that holds references to Widget arrays. The ONLY thing you can put in here is an array of Widgets. Again, using polymorphisim, if Foos are Widets, and Bars are Widgets, you could put an array of either into your Widget array.

A 3-d Widget array is an array that holds references to (Arrays that hold references to (Widget arrays)).

so it doesn't makes sense to say "one dimension holding Integers and the other holding Strings".

 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic