[04] array initialization

java and C # are very similar, most of their syntax is the same, but nevertheless, there are some places is different.

In order to better learn java or C #, it is necessary to distinguish exactly where they are both different.

This time we want to come explore initialize the array .

 

java code:

 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 # code:

 

 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 }

 

Analysis and conclusion:

1, java in the definition of the array when the brackets can be placed before the variable name can also be placed after the variable name, and C # can only be placed before the variable name.

2, C # may indicate both the length and the definition of variables initialized simultaneously, but can not do so java.

 

Thanks for watching!

 

 

Guess you like

Origin www.cnblogs.com/edcoder/p/12000712.html