• Post Reply Bookmark Topic Watch Topic
  • New Topic

BubbleSort on Matrix  RSS feed

 
john narciso
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys please help me. I got this code to sort the number of the elements of a matrix. It says Process completed when I build the file then after I run it, it is still ok until I reach the SORT section where i got the error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at MatrixAll.main(MatrixAll.java:77)

Can anyone help me on this problem on wherever my program got an error? Thanks in advance ^_^


 
Myyron Murray Latorilla
Ranch Hand
Posts: 65
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first glance in your code the

in the if statement is the culprit, when you get to the last iteration of your for loop, this test of condition will certainly throw the exception. Provide another if statement before this code to test if the row/column index is already length-1.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John. Welcome to the Ranch!

ArrayIndexOutOfBoundsException means that you're trying to access an element of the array that doesn't exist. So let's look at the line that's causing it...
john narciso wrote:

What's going to happen when you reach y = column - 1? That's the last column, but you're then trying to access the one after it.

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say the user entered 10 as the number of rows. That means the for loop on line 73 will loop from 0 to 9 inclusive which is good because a 10 element array is indexed from 0 to 9.
However you're not accessing element x of the array, you're accessing element x + 1, so what happens when x equals 9 ?
 
john narciso
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not so good at programming and I just tried to copy this program on how I understand it that's why I got that code m[x+1][y+1] haha XD

but this is an array only

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john narciso wrote:but this is an array only

Maybe, but it's worth remembering that almost ALL Java indexes are 0-based, even in some places where you don't want it (like Calendar.MONTH!!!).

Just get used to it; otherwise it'll keep slapping you in the face. You may even learn to love them as you get more used to them.

Winston
 
john narciso
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys I tried this now but I got a problem.



But I got this on my output
Sorted Number: [I@19821f [I@addbf1
what will I do to fix it?
 
john narciso
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BUMP !! Help please thanks
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, my kababayan,

You don't seem to understand the basics of arrays and using them. I suggest that you start with something simple first. Then work your way up to this problem. Here's an exercise for you to try:

Array Indexing Basics. Check your understanding of how array indexing works. First, predict the output of this program. Then, compile and run the program to see if you predicted correctly.
 
john narciso
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I understand array correctly but I can't program it correctly haha sorry for the noob question and thanks
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john narciso wrote:I think I understand array correctly but I can't program it correctly haha sorry for the noob question and thanks

Programming is simply a very formal way of expressing your understanding of a problem solution. If you can't program it, that means you don't understand it. Did you try the exercise? Did you predict correctly? I added one more line of output, BTW.
 
john narciso
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haha the System.out.println(data); output displays "[I@19821f" like the one I have on my program... If this is only an array I think I can do it properly haha I hate Matrix XD
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are a few things you need to understand.

1) Java doesn't have anything called a matrix. What you are doing is using arrays to simulate a matrix.

2) There are NO SUCH THINGS as multi-dimensional arrays in java. There are only single dimensional arrays.

3) When you think I am wrong about #2, read it again, and understand that I am right.

4) A 1-D array in java can hold primitives (like ints or floats) or objects.

5) An array is an object.

6) An array can therefore hold arrays.

So, when you say "System.out.print(m[ctr] + " "); ", you are saying "print the m'th thing in the array called 'm'". And that thing is an array - which java does not know how to print (at least, not how YOU want it to print).

What would you expect it to show you? Remember, an array can potentially hold ANYTHING - so there is no possible way the jvm can know what you want it to do. You PROBABLY want it to print all the elements in that array, but if so, you will have to write that code.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!