This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Yet another array problem :P Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Yet another array problem :P" Watch "Yet another array problem :P" New topic

Yet another array problem :P

Brent Lancelot

Joined: Apr 01, 2006
Posts: 8
well heres some more code with an array and im trying to multiply the matrixes of two arrays, i think its when i try to assign values to the array d and pass it back. it says the array is out of bounds but ive made sure to check everything and it seems just fine all the array have 5 elements and im not trying to access nething over that, it also comes up excepttion in thread main
yay i love programming

package Chapter5;
//Ex 5.23
public class MultiplyMatrix {
public static void main(String[] args) {
int[][] a = {{3,4,7,3,2},{4,6,2,9,1},{7,8,1,2,6},{1,3,2,9,7},
int[][] b = {{7,2,9,6,1},{4,8,1,3,9},{1,5,9,2,7},{8,4,1,6,2},
int[][] c = new int[4][4];
int x=0, y=0;

c = mMatrix(a,b);
for(x=0; x<5; x++)
for(y=0; y<5; y++)
System.out.print(c[x][y] + " ");

public static int[][] mMatrix(int[][] a, int[][] b)
int[][] d = new int[4][4];
int i=0, j=0, z=0;

for(i=0; i <5; i++)
z = (a[i][0] * b[0][j]) + (a[i][1] * b[1][i]) + (a[i][2] * b[2][i]) +
(a[i][3] * b[3][i]) + (a[i][4] * b[4][i]);
d[i][j] = z;
return d;
Brent Lancelot

Joined: Apr 01, 2006
Posts: 8
oh no nevermind its the array my bad
Henry Wong

Joined: Sep 28, 2004
Posts: 18545

The 4 specifies the number of elements. This is an array of 4 arrays with 4 ints each.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Brent Lancelot

Joined: Apr 01, 2006
Posts: 8
yeah i keep thinking when i declare an array i need to count 0 as an element
I agree. Here's the link:
subject: Yet another array problem :P
Similar Threads
How do I handle "The Matrix" ?
AOOB exception in my CODE-PLEASE HELP!
Doubt on KB Question #12 Chapter 5
how to complete this code?
Chapter 5 K&B -Doubt