• 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

2d array help

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm new here and I have some quick questions:

I want to print a table (array) where I have an X axis and a Y axis. 16x11. My code is as follows:



The code doesn't work even though it's similar to



Can anyone explain why? if I remember correctly, the 2 are correct just written in different ways.

and I want it to print like this


where 0 is null (for now). I want to fill those in with a formula later, but I don't hardcoding them one by one is the most "efficient" way to do it

Anyone mind helping?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both are quite a lot different.

The first one is trying to mimic the use of initialization block, but then its not a valid syntax of initialization block.

The second one is initializing the array along with defining it. But the first is is defining it correctly but its a syntactically incorrect approach to initialize it.

In the First one you have an additional semi colon at the end of the definition. Removing that semicolon should make it equivalent to second approach
 
Gabe Evan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:Both are quite a lot different.

The first one is trying to mimic the use of initialization block, but then its not a valid syntax of initialization block.

The second one is initializing the array along with defining it. But the first is is defining it correctly but its a syntactically incorrect approach to initialize it.



I see. Thank you. I kept tweaking it and went with



and now my output is



It's looking better but can you tell me why that last 0 gets picked up on the first row? It's supposed to be the first number in the second row. I'm playing around with a if statement right now
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gabe Evan wrote:
and now my output is



It's looking better but can you tell me why that last 0 gets picked up on the first row? It's supposed to be the first number in the second row. I'm playing around with a if statement right now



There is no second row. Your application puts everything into one long row. Any "second row" is caused by your terminal going to the next line, when there isn't any more room.

Henry
 
Gabe Evan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, I feel like an idiot now.

I keep getting




How do I get




my loop is


It's a mess
 
Gabe Evan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I fixed it. It was a simple error.



Which prints



If I want that 2nd row to be displayed vertically, do I need to make that specific section into a new array?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Variable names should begin with a lower case character.
This means, of course, that all other rows only have a length of 1.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would actually suggest writing them in two seperate loop rather than a nested loop.

ie:
write out x axis values, separated by tabs all on one line
write out y axis values, one per line.

I'm not certain a two dimensional array is appropriate for this. I think it would be better modeled as two separate arrays.
ie:

int[] xAxis = {60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0, -5, -10, -15};
int[] yAxis = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};

alternatively as the scales are linear you might dispense with these arrays completely and specify an axis with "startNumber", "step" and "howMany"
e.g. your x axis could be specified as:
startNumber: 60
step: -5
howMany: 16 (or endNumber: -15)


You will still need the array of arrays for the actual values.
 
Gabe Evan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:I would actually suggest writing them in two seperate loop rather than a nested loop.

ie:
write out x axis values, separated by tabs all on one line
write out y axis values, one per line.

I'm not certain a two dimensional array is appropriate for this. I think it would be better modeled as two separate arrays.
ie:

int[] xAxis = {60, 55, 50, 45, 40, 35, 30, 25, 20, 15, 10, 5, 0, -5, -10, -15};
int[] yAxis = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50};

alternatively as the scales are linear you might dispense with these arrays completely and specify an axis with "startNumber", "step" and "howMany"
e.g. your x axis could be specified as:
startNumber: 60
step: -5
howMany: 16 (or endNumber: -15)


You will still need the array of arrays for the actual values.



Thank you for the help. I'm doing this as practice and one of the "objectives" was doing this with a 2D array, are you sure it can't be done?
I plan on filling in the spaces with numbers from the formula so the end array will be something like


Hope you get the gist
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost anything can be done.
I am just of the opinion that your axes should be defined separately to your data.

When you present it on screen, sure you want them in that grid format, the first row being your x axis values, the first column being your y axis values.
But that doesn't necessitate that you STORE them in all within the same data structure.

So i would define it as



Just my 2 cents of course :-)

 
Gabe Evan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:Almost anything can be done.
I am just of the opinion that your axes should be defined separately to your data.

When you present it on screen, sure you want them in that grid format, the first row being your x axis values, the first column being your y axis values.
But that doesn't necessitate that you STORE them in all within the same data structure.

Just my 2 cents of course :-)






Definitely. At first I was wondering how it would work with a 2D, but now that you affirmed 2 separate arrays, I decided to go with it. It actually formatted easily the way I wanted it to.
 
reply
    Bookmark Topic Watch Topic
  • New Topic