Interview(9)Java Array and Other

Interview(9) Java Array and Other

Java Array
int intArray[] = {1, 2, 3, 4};
float floatArray[] = new float[3];
floatArray[0] = 1.0f;
floatArray[1] = 132.63 f;
floatArray[2] = 100f;

int arrayDemo[] = {1, 2, 4, 7, 9, 192, 100};
for(int x: arrayDemo){
    System.out.println(x + ", ") ;
}

int intArray[][] = { {1, 2}, {2, 3}, {4, 5}};
int a[][] = new int[2][3];
a[0][ 0] = 12;
a[0][1] = 34;
..snip…
a[1][2] = 93;

2 Array Result
// first matrix (dynamically initialized a 2D array)
int a[] [] = new int[2][3];
// second matrix (statically initialized a 2D array)
int b[][] = { {1,5,2,8}, {5,9,10 ,-3}, {2,7,-5,-18} };
// result matrix
int c[][] = new int[2][4];
// initialize first matrix
for(int i=0; i<2; i++)
    for(int j=0; j<3 ;j++)
            a[i][j] = (i+1) * (j+2);
// compute matrix product
for (int i=0; i<2; i++){
    for (int j=0; j <4; j++){
            c[i][j]=0;
     for(int k=0; k<3; k++)
                c[i][j] += a[i][k] * b[k] [j];
    }
}
// output the settlement result
for(int i=0; i<2; i++){
for (int j=0; j<4; j++)
    System.out.printf("%-5d", c[i][j]);
    System.out.println();
}

Mathematics in college, linear algebrahttp
:


//www.ruanyifeng.com/blog/2015/09/matrix-multiplication.html References:
https://leetcode.com/problemset/all/
https://github.com/OrionMedivh

http://www.weixueyuan.net/view/6316.html
http://www.ruanyifeng.com/blog/2015/09/matrix-multiplication.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326353751&siteId=291194637