簡體   English   中英

如何在數組[][,]和[,][]中輸入和輸出值(交錯數組和多維數組的混合)

[英]How to input and output values in array [][,] and [,][] (mix of jagged array and multidimensional array)

我正在嘗試改進有關如何在多維數組中查找項目的代碼,因為我想在增加數據量時避免將來可能出現的性能問題。 我是編程新手,所以有很多我不知道的東西。 我一直在圍繞多維數組、鋸齒狀數組、排序等主題進行大量搜索。 我想我需要使用鋸齒狀數組,因為我需要排序才能找到第三大和 6.largest 的數字。 但是我意識到我必須就示例尋求一些幫助或鏈接到更多信息,因為我在定義我的鋸齒狀數組時遇到了問題。 我將嘗試隔離每個問題,因為我陷入了一些我認為對於比我更熟悉數組的人來說可能很容易的事情。 應該可以根據鋸齒狀數組混合鋸齒狀和多維數組

這是 [][] 的示例,它正在工作

using System;
using System.Collections;
namespace SortJaggedArray
{
class host
{
    [STAThread]
    static void Main(string[] args)
    {
        int[][] arr = new int[2][];
        arr[0] = new int[3] {1,5,3};
        arr[1] = new int[4] {4,2,8,6};

        // Write out a header for the output.
        Console.WriteLine("Array - Unsorted\n");

        for (int i = 0; i < arr.Length; i++)
        {
             System.Console.WriteLine("Outer array " + i);

             for (int j = 0; j < arr[i].Length; j++)
             {
                  System.Console.Write(arr[i][j] + " ");
             }
             System.Console.WriteLine(" ");
             System.Console.WriteLine(" ");
        }
        Console.ReadLine();
    }
}
}

//Output:
//Outer array 0
//1 5 3

//Outer array 1
//4 2 8 6

這是我輸入工作的 [][,] 示例,但我在如何編寫輸出時遇到了困難:

using System;
using System.Collections;
namespace SortJaggedArray
{
    class host
    {
        [STAThread]
        static void Main(string[] args)
        {
            int[][,] arr = new int[2][,]
            {
                new int[,] { { 1, 3 }, { 5, 2 }, { 3, 9 } },
                new int[,] { { 4, 1 }, { 2, 7 }, { 8, 5 }, { 6, 3 } }
            };
            // Write out a header for the output.
            Console.WriteLine("Array - Unsorted\n");

            foreach (int i in arr)
                Console.WriteLine(i);

            Console.ReadLine();
        }
    }
}

Wanted output:
Nr 0: 
1, 3
5, 2
3, 9

Nr 1:
4, 1
2, 7
8, 5
6, 3

問題一:WriteLine / for / foreach 怎么寫才能看到鋸齒狀數組 [][,] 的內容?

問題 2:我想將其更改為 [,][] 但是我遇到了如何在這種鋸齒狀數組中輸入/輸出數據的問題。 如何輸入數據? 如何寫 / for / foreach 來查看鋸齒數組 [,][] 的內容?

您需要遍歷每個維度:

for(int i=0; i<arr.Length; i++){
    Console.WriteLine($"Nr {i}:");
    for(int j=0;j<arr[i].GetLength(0);j++){
        for(int k=0;k<arr[i].GetLength(1);k++){
            Console.Write($"{arr[i][j,k]} ");
        }
        Console.WriteLine();
    }
    Console.WriteLine();
}

輸出:

Nr 0:
1 3 
5 2 
3 9 

Nr 1:
4 1 
2 7 
8 5 
6 3 
package array;
import java.util.Scanner;
public class multiDimensionalArray {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int i, j;
        int values[][]=new int [3][3];
        System.out.println("Enter array values");
        for(i=0; i<3; i++) {
            for(j=0; j<3; j++) {
                values[i][j]=sc.nextInt();
                }
        }
            System.out.println("Entered values are:  \t");
            for(i=0; i<3; i++) {
                for(j=0; j<3; j++) {
                    System.out.print("\t"+values[i][j]);
        }
                System.out.print("\n"); 
        
        }
    
            }
            
    }
    

        
    

如果其他讀者有同樣的問題,我想添加代碼示例,說明磁控管的示例如何幫助我解決數組 [,][] 的相同問題

using System;
using System.Collections;
namespace SortJaggedArray
{
    class host
    {
        [STAThread]
        static void Main(string[] args)
        {
            int[,][] arr = new int[2,3][];
            arr[0,0] = new int[3] { 1, 5, 3 };
            arr[0,1] = new int[4] { 4, 2, 8, 6 };
            arr[0,2] = new int[2] { 2, 8 };
            arr[1,0] = new int[2] { 7, 5 };
            arr[1,1] = new int[5] { 8, 7, 5, 9, 2 };
            arr[1,2] = new int[2] { 1, 4};

            // Write out a header for the output.
            Console.WriteLine("Array - Unsorted\n");

            for (int i = 0; i < arr.GetLength(0); i++)
            {
                Console.WriteLine($"Nr {i}:");
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    for (int k = 0; k < arr[i,j].Length; k++)
                    {
                        Console.Write($"{arr[i,j][k]} ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

//Output:
//Nr 0:
//1 5 3
//4 2 8 6
//2 8

//Nr 1:
//7 5
//8 7 5 9 2
//1 4

暫無
暫無

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

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