【04】数组的初始化

java和C#非常相似,它们大部分的语法是一样的,但尽管如此,也有一些地方是不同的。

为了更好地学习java或C#,有必要分清它们两者到底在哪里不同。

我们这次要来探讨数组的初始化

 

java代码:

 1 package HelloWorld;
 2 
 3 public class HelloWorld {
 4     public static void main(String[] args) {
 5         int a1[] = {1, 2, 3}; //特有
 6         int[] a2 = {1, 2, 3};
 7         int[] a3 = new int[]{1, 2, 3};
 8         int[] a4 = new int[5];
 9         a4[0] = 1;
10         a4[1] = 2;
11         a4[2] = 3;
12         for (int i = 0; i < a4.length; i++) {
13             System.out.println(a4[i]);
14         }
15     }
16 }

 

C#代码:

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApp1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int[] a1 = { 1, 2, 3 };
14             int[] a2 = new int[] { 1, 2, 3 };
15             int[] a3 = new int[3] { 1, 2, 3 }; //特有
16             int[] a4 = new int[5];
17             a4[0] = 1;
18             a4[1] = 2;
19             a4[2] = 3;
20             for (int i = 0; i < a4.Length; i++)
21             {
22                 Console.WriteLine(a4[i]);
23             }
24 
25             Console.ReadKey();
26         }
27     }
28 }

 

分析和总结:

1、java在定义数组的时候,方括号可以放在变量名前也可以放在变量名后,而C#只能放在变量名前。

2、C#可以在定义变量时既指明长度又同时初始化,但java不能这样做。

 

谢谢观看!

 

 

posted @ 2019-12-07 09:49  the_path_of_grace  阅读(236)  评论(0编辑  收藏  举报