![]() |
|
#1
|
|||
|
|||
but... I thought I understood arrays!Hi, I am new here. Both to the forum, and to Java. This is my first Java program, and I'm having issues with my arrays, of all things! Please note that I know this is spaghetti code, but I haven't had time to add "object orientation" to my program... I'm still working on understanding the stuff I'm calling.
The code below works, but as you read in the comments up top, if you change my arrays back to [5] and/or [5][2] respectively, i get an array runtime error. Feel free to ask questions, and thank you all sooooo much in advance for helping. Also, feel free to comment on any other mistakes... I'm sure there are plenty. I need more friends who speak java! JAVA Code:
|
|
#2
|
|||
|
|||
Re: but... I thought I understood arrays!Check the argument when calling addHeatElement().
addHeatElement(i) will use its argument to access myPanels[i], and if you have wrote Code:
Remember that index numbering starts with zero. The myPanels array as above has 8 elements: 0, 1, 2, 3, 4, 5, 6 and 7. |
|
#3
|
|||
|
|||
Re: but... I thought I understood arrays!Ok, here is a shorter explanation
JAVA Code:
It errors out because when J hits "2" i says arrayindexoutofbounds. I even added debugging code and watched J go in as 2... throwing the error.... strange (see full code above). Thanks! Last edited by stephano : 22-Feb-2007 at 09:57.
Reason: clarification
|
|
#4
|
|||
|
|||
Re: but... I thought I understood arrays!That's because when you declare:
JAVA Code:
There exists only 2 in that array... index 0 and index 1. There is no index 2, since there are only 2 elements ( 0 and 1 ). So 2 is "out of bounds". Does that make sense? So you have: myButtons[0][0], myButtons[0][1] myButtons[1][0], myButtons[1][1] etc. There is no myButtons[0][2], that is out of bounds. Did that help? |
|
#5
|
|||
|
|||
Re: but... I thought I understood arrays!Thank you!
I feel awful silly. I started counting from zero when I didn't need to. Arrays sometimes hurt my head; esp. multi-dimensional arrays! Thanks again! |
Recent GIDBlog
Developing GUIs with wxPython (Part 4) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic vs Static Arrays | WaltP | Miscellaneous Programming Forum | 5 | 16-Feb-2006 16:54 |
| Noob question on c arrays and functions | brett | C Programming Language | 1 | 20-Apr-2005 04:59 |
| Knight tour (arrays help needed) | dilmv | C++ Forum | 7 | 18-Oct-2004 15:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The