2014-09-20 53 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)); 
}