簡體   English   中英

無法列印我的清單

[英]Trouble printing my list

如果您看到我在下面的矩陣中保留數字的位置,則按照我的意願列出了所有這些數字,當我嘗試打印該列表時,請向我發送很多這些信息:I @ 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);
    }
}

雖然我看不到您在哪里打印出這樣的列表:

System.out.println(ll);

...由於要在未綁定的LinkedList內添加多維數組,因此在打印它們時必須格外小心。

首先:您必須自己遍歷列表。

for(Object value : ll) {

}

下一步:由於您沒有泛型,因此必須強制轉換。 如果將LinkedList鍵入LinkedList<int[][]>會使事情變得更容易。

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

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

我相信您在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM