2014-09-20 50 views
0

如果您看到我在下面的矩陣中保留的數字是根據我的清單,還有所有這些數字,並且當我嘗試打印清單時,只需發送給我很多這些清單: 我@ 7852e922這是內存地址?並且如果打印的對象。感謝您的關注。無法打印我的清單

package estructurasDeDatos; 

import java.util.List; 

import java.util.LinkedList; 

public class Matriz { 

    private static Scanner read; 

    public static void main(String[] args){ 
     /*read = new Scanner(System.in); 
     System.out.println("Ingrese el numero de filas y columnas: "); 
     int f = read.nextInt(); 
     int c = read.nextInt();*/ 

     LinkedList ll = new LinkedList(); 

     System.out.println("  ** **  *** ******* ****  ***** *** ***** ***** "); 
     System.out.println(" * ** * * *  * * *  *  *  *  *  "); 
     System.out.println(" * ** * *  * * * *  * *  *  *  "); 
     System.out.println(" *  * ******* * ****  * *  ***  **** "); 
     System.out.println(" *  * *  * * * *  * *  *   * "); 
     System.out.println(" *  * *  * * * *  *  *  *   * "); 
     System.out.println(" *  * *  * * *  * ***** *** ***** ***** "); 
     System.out.println("###########################################################################"); 
     int mt [][] = new int[10][10];//Genero la matriz con dos arreglos con una cantidad que yo quiera de 
     //filas y columnas. 

     int num; 
     for (int i = 0; i < 10; i++) 
     { 
      for (int j = 0; j < 10; j++) 
      {//Genero un numero aleatorio entre 0 y 100 
       num=(int)(Math.random()*100); 

       //Asigno el numero generado a la matriz 
       mt[i][j]=num; 
       ll.push(mt); 
      } 
     } 

     for (int i = 0; i < 10; i++) 
     { 
      for (int j = 0; j < 10; j++) 
      {//Imprimo la celdad de la matriz 

       System.out.print(mt[i][j]+"\t"); 
      } 

      System.out.println(); 
     } 
     System.out.println("###########################################################################"); 
     System.out.println("\n\n"); 
     System.out.println("LOS NUMEROS DE LA DIAGONAL PRINCIPAL:"); 
     System.out.print(mt[0][0]+" "+mt[1][1]+" "+mt[2][2]+" "+mt[3][3]+" "+mt[4][4]+" "+mt[5][5]+" "+mt[6][6]+" "+mt[7][7]+" "+mt[8][8]+" "+mt[9][9]+"\n\n"); 

     int sumatoriadiap=mt[0][0]+mt[1][1]+mt[2][2]+mt[3][3]+mt[4][4]+mt[5][5]+mt[6][6]+mt[7][7]+mt[8][8]+mt[9][9]; 

     System.out.println("La sumatoria de la diagonal principal es: \n"+sumatoriadiap); 

     System.out.println("El promedio de la diagonal principal es : \n"+sumatoriadiap/10); 
     //////////////////////////////////////////////////////////////////////////////////////////////////////// 
     System.out.println("LOS NUMEROS DE LA DIAGONAL SECUNDARIA:"); 
     System.out.print(mt[0][9]+" "+mt[1][8]+" "+mt[2][7]+" "+mt[3][6]+" "+mt[4][5]+" "+mt[5][4]+" "+mt[6][3]+" "+mt[7][2]+" "+mt[8][1]+" "+mt[9][0]+"\n\n"); 

     int sumatoriadias=mt[0][9]+mt[1][8]+mt[2][7]+mt[3][6]+mt[4][5]+mt[5][4]+mt[6][3]+mt[7][2]+mt[8][1]+mt[9][0]; 

     System.out.println("La sumatoria de la diagonal principal es: \n"+sumatoriadias); 

     System.out.println("El promedio de la diagonal principal es : \n"+sumatoriadias/10); 
    } 
} 
+0

對於初學者來說,我沒有看到你在哪裏」重新列印清單。另外,你正在處理沒有'toString()'重寫的數組 - 你看到的是數組的哈希碼。第三,你的鏈表沒有類型,這使得它工作......有趣...... – Makoto 2014-09-20 06:54:04

回答

0

我相信你添加了錯誤的元素,你LinkedList

for (int j = 0; j < 10; j++) { 
    num = (int) (Math.random() * 100); 
    mt[i][j] = num; 
    ll.push(num); //add the random number to the LinkedList instead of the whole 2 dime array 
} 

然後打印添加類似:

System.out.println(ll); 
+0

非常感謝這項工作 – Cheko 2014-09-22 09:36:15

0

雖然我沒有看到你打印出您的列表如下:

System.out.println(ll); 

...因爲你要添加的未綁定LinkedList內部多維數組你必須要特別小心打印它們。

首先:你必須自己迭代列表。

for(Object value : ll) { 

} 

Next:因爲您沒有泛型,所以必須進行施放。如果您輸入LinkedListLinkedList<int[][]>,則會使事情更輕鬆。

然後,您必須使用Array#deepToString,它將以可打印和可表示的方式將深嵌套值從數組中取出。

for(Object value : ll) { 
    System.out.println(Arrays.deepToString((int[][]) value)); 
}