Java二維數組和C#二維資料比較

來源:互聯網
上載者:User

標籤:threading   ext   console   ppa   列印   技術分享   數組   images   sys   

1. Java的二維數組在實際應用中使用是非常廣泛的。下面樣本示範使用二維數組實現列印一組資料中的最大值和最小值。

public class MutiArray{   int theValues[][]={        {12,23,33,56,26},        {9,87,68,55,26},        {36,88,98,43,19},        {25,16,8,37,99}      };       //求二維數組中最小元素的方法    public int minimum()    {        int lowTheValue=theValues[0][0];        for (int row=0;row<theValues.length;row++)        {             for(int column=0;column<theValues[row].length;column++)             {                 if (lowTheValue > theValues[row][column])                 {                     lowTheValue=theValues[row][column];                 }             }        }        return lowTheValue;    }        //求二維數組中最大元素的方法    public int maximum()    {        int highTheValue=theValues[0][0];        for (int row=0;row<theValues.length;row++)        {             for(int column=0;column<theValues[row].length;column++)             {                 if (highTheValue < theValues[row][column])                 {                     highTheValue=theValues[row][column];                 }             }         }         return highTheValue;    }            //主方法    public static void main(String[] args)    {         MutiArray item = new MutiArray();         System.out.println("數組中的最小元素是: " + item.minimum());         System.out.println("數組中的最大元素是: " + item.maximum());    }      } 

2. 現在再看看C#中處理相同值的二維數組,其代碼如下所示:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleAppArray{    public class MutiArray    {       public static int[,] mutiArray = new int[4,5]{                    {12,23,33,56,26},                    {9,87,68,55,26},                    {36,88,98,43,19},                    {25,16,8,37,99}                };    //定義二維數組並初始化        //求二維數組中最小元素的方法        public static int minimum()        {            int lowMutiArray = mutiArray[0, 0];            for(int row=0;row <mutiArray.GetLength(0);row++)            {                for (int column = 0; column < mutiArray.GetLength(1); column++)                {                    if(lowMutiArray > mutiArray[row, column])                    {                        lowMutiArray = mutiArray[row, column];                    }                }            }            return lowMutiArray;        }        //求二維數組中最大元素的方法        public static int maximum()        {            int highMutiArray = mutiArray[0, 0];            for(int row = 0; row < mutiArray.GetLength(0); row++)            {                for(int column = 0;column < mutiArray.GetLength(1); column++)                {                    if(highMutiArray < mutiArray[row, column])                    {                        highMutiArray = mutiArray[row, column];                    }                }            }            return highMutiArray;        }    }    class Program    {        static void Main(string[] args)        {            //遍曆二維數組            foreach(int item in MutiArray.mutiArray)            {                Console.Write(item + ",");            }            Console.WriteLine();            Console.WriteLine("二維數組中最小的元素是:" + MutiArray.minimum());            Console.WriteLine("二維數組中最大的元素是:" + MutiArray.maximum());            Console.ReadLine();        }    }}

 執行結果如所示:

Java二維數組和C#二維資料比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.