Results 1 to 7 of 7
Thread: Bounds problem...
- 10-21-2010, 05:27 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
[solved]Bounds problem...
So I'm being told that I have a bounds problem and I know why, but I can't figure out how to set appropriate bounds. I'm trying to do a steady state temperature distribution, but I'm unsure on how to actually create bounds which work. I know I need one for the center points, one for the boarder points, and one for the corners. Anyone think they could help me with this?
Java Code:int length = 20; int width = 20; double hotplate[] [] = new double[length] [width]; ((Each of the hotplate arrays have been filled...)) for (int row = 1; row < length; row++){ for (int column = 1; column < width; column++){ int left = row + 1; int right = row - 1; int up = column + 1; int down = column - 1; hotplate[row] [column] = ((left + right + up + down)/4); hotplate[row] [column] = ((hotplate[left] [column] + hotplate[right] [column] +hotplate[row] [up]+hotplate[row] [down])/4); System.out.print("|"+hotplate[row] [column]+"|"); } System.out.println(""); }Last edited by licka; 10-21-2010 at 09:48 PM. Reason: Solved
- 10-21-2010, 06:01 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
You might need to tell us a bit more; in fact, a whole lot more -- what problem are you trying to solve, what is wrong with your current code, etc....
- 10-21-2010, 06:11 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
I'm taking a 2D array (20x20) with all the values set to 0 except for the top and bottom line which have 100's in each of the holders. I'm trying to take an array (say array[2] [2]) and changing it to the average of the array directly above it, below it, and on either side of it. I'm doing this until no cell in the array changes more than 0.1 degree. So what I need help with if figuring out how to create bounds for the outter edges, corners, and interior calculations... My actual error is: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
- 10-21-2010, 07:17 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
Java Code:for (int row = 1; row < length; row++){ for (int column = 1; column < width; column++){ ... hotplate[row] [column] = ...
Array indices in Java are zero based: ie if I have an array of length 7, the array indices run from zero to six.
See Arrays in Oracle's Tutorial.
- 10-21-2010, 07:34 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
I've actually read that quite a bit :D I'm glad to know I was at least looking in the right place.
Originally Posted by pbrockway2
I'm aware that the values for a 20x20 array would be 0-19. I have row = 1 and column = 1 because I don't want it to try reading the 0 values.
So rather than reading 0, it reads the 1. It was an attempt to avoid part of my border problem. If I had it at 0 my left and up would have problems.Java Code:0 100 100 100 100 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 100 100 100 100 0
- 10-21-2010, 08:11 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
OK - Sorry, I wasn't reading closely enough. You take care of one end by starting at index 1, but there is still a problem at the other end where you use indices left and up which are one greater than the index values set by the loop.
In the case of a 7x7 plate only the cells marked * can have their new value set as an average of their neighbours. Ie they form a 5x5 collection. In the general case wouldn't this correspond to something like the folllowing?:Java Code:0 100 100 100 100 100 0 0 * * * * * 0 0 * * * * * 0 0 * * * * * 0 0 * * * * * 0 0 * * * * * 0 0 100 100 100 100 100 0
Java Code:for (int row = 1; row < length-1; row++){ for (int column = 1; column < width-1; column++){
(Also you might like to confirm that the new value is the average of the 4 neighbours - or describe what the rule is.)
- 10-21-2010, 09:38 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 6
- Rep Power
- 0
Thank you for that! I tried setting a while loop for row being less than 19 and column being less than 19, then I tried to simply place a fixed number where length and width were and that wasn't working either. (Though I don't know the latter didn't work :confused: ) But I didn't even think of having an equation being equal to row and column. That's something I'll have to remember. Thank you so much for showing me that you can use equations on a greater than or less than problem! :)
Similar Threads
-
Out of bounds exception Errors (Trouble finding problem)
By sidd0123 in forum New To JavaReplies: 12Last Post: 05-06-2010, 10:54 AM -
array going out of bounds?
By jabo in forum New To JavaReplies: 9Last Post: 04-02-2010, 10:08 AM -
Array Index Out Of Bounds and Problem in Assigning Values
By chronoz1300 in forum New To JavaReplies: 2Last Post: 12-28-2009, 07:14 PM -
[SOLVED] Search problem - array out of bounds
By viper110110 in forum New To JavaReplies: 5Last Post: 11-26-2008, 04:26 AM -
why is my array out of bounds?
By Phobos0001 in forum New To JavaReplies: 3Last Post: 03-24-2008, 01:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks