2011-09-27 94 views
0

中獲取值,每次我對y執行一個操作時,從表中獲取值0和1。java:從矩陣

String[][] columns = {{"col1" , "col2"}}; 
int[] values = new int[columns.length]; 
String[] y = { "TEST", "BUG" }; 

    for (int j = 0; j < y.length; j++) 
    { 
     //do some actions 
     //bellow I need to get at the same time col1 and col2  
     table.getVal(0, columns [0][j])) ? 
    } 

我需要在y1和y2上得到col1,col2的值嗎?我如何使用getVal中的列來獲得預期的值?

感謝,

回答

2
class MatrixExampleDemo { 
    public static void main(String[] args) { 
     int array[][] = { { 1, 3, 5 }, { 2, 4, 6 } }; 
     System.out.println("Row size= " + array.length); 
     System.out.println("Column size = " + array[1].length); 
     outputArray(array); 
    } 

    public static void outputArray(int[][] array) { 
     int rowSize = array.length; 
     int columnSize = array[0].length; 

     for (int i = 0; i <= 1; i++) { 
      System.out.print("["); 
      for (int j = 0; j <= 2; j++) { 
       System.out.print(" " + array[i][j]); 
      } 
      System.out.println(" ]"); 
     } 
     System.out.println(); 
    } 
} 

入住這也...................

import java.lang.reflect.Array; 
import static java.lang.System.out; 

public class CreateMatrix { 
    public static void main(String... args) { 
     Object matrix = Array.newInstance(int.class, 2, 2); 
     Object row0 = Array.get(matrix, 0); 
     Object row1 = Array.get(matrix, 1); 

     Array.setInt(row0, 0, 1); 
     Array.setInt(row0, 1, 2); 
     Array.setInt(row1, 0, 3); 
     Array.setInt(row1, 1, 4); 

     for (int i = 0; i < 2; i++) 
      for (int j = 0; j < 2; j++) 
       out.format("matrix[%d][%d] = %d%n", i, j, ((int[][])matrix)[i][j]); 
    } 
} 
+0

謝謝,但我真的不明白你的解決方案 – lola

+0

你甚至嘗試瞭解他的解決方案? – Joze

+0

我已經更新了我的問題,我的矩陣是列 – lola

0

我不太看邏輯在您的代碼中,但一般矩陣操作將是:

String[][] matrix = new String[10][10]; 
matrix[0][0] = "1.1"; 
matrix[0][1] = "1.2"; 
matrix[1][0] = "2.1"; 
matrix[2][1] = "2.2"; 

int x = 0; // col 
int y = 1; // row 
String val = matrix[y][x]; // 1.2 
+0

我只需要獲取y1和y2上的列值,並且需要在boucle之後獲取這些值才能將它們用於其他計算。 – lola

+0

@lola,我已經給出了一個關於如何在java中操作矩陣的演示。我敢肯定*你可以從這個示例中學習如何將它合併到你自己的代碼中。 –

+0

我用矩陣列更新了問題 – lola