201,604,232-D arrays, jagged arrays, and collections

Source: Internet
Author: User

2016-04-23 20:46:26

A. Two-dimensional array:

One-dimensional array---beans

Two-dimensional array---table

1) Definition:

One-dimensional arrays:

data type [] array variable name = new data type [array length];

data type [] array variable name = new data type [array length]{1,2,3 ...};

2) Two-dimensional array:

data type [,] array variable name = new data type [number of rows, number of columns];

int[,] a = new int[3,4];

Assignment value:

a[row subscript, column subscript] = value subscript is starting from 0

Value:

a[row subscript, column subscript]

☆. Title: A class of 6 people, from the keyboard input each study number language, mathematics, foreign language results (no need to enter the number). Output: Student score table (including each person's total score), the average score for each section.

Add 1: Try to show the failing words in red.

Add 2: Try to sort by the total score, show the ranking out.

int[,]a=New int[6,5];  for(inti =0; I <6; i++) {Console.WriteLine ("Enter the language score for the {0} students:", i+1); intLanguage Achievement =Convert.ToInt32 (Console.            ReadLine ()); Console.WriteLine ("Enter the math score for the first {0} students:", i +1); intMath score =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("Enter the English score for the {0} students:", i +1); intEnglish score =Convert.ToInt32 (Console.ReadLine ()); A[i,0] =language achievement; A[i,1]=mathematical results; A[i,2] =English performance; A[i,3] = Language score + Math score +English performance; }         for(inti =0; I <6; i++ )        {             for(intj = i; J <5; j + + )            {             if(A[i,3]>a[j+1,3])            {                intv = a[i,3]; A[i,3] = A[j +1,3]; A[j+1,3] =v; intm = A[i,0]; A[i,0] = A[j +1,0]; A[j+1,0] =m; intn = a[i,1]; A[i,1] = A[j +1,1]; A[j+1,1] =N; intB= A[i,2]; A[i,2] = A[j +1,2]; A[j+1,2] =b; }                        }        }        intSum language =0, Sum math =0, Sum English =0, Avg language, Avg math, avg English; Console.Write ("Chinese \ t math \ t english \ t total score \ t Sort");        Console.WriteLine ();  for(inti =0; I <6; i++) {Console.WriteLine ("{0}\t{1}\t{2}\t{3}\t{4}", A[i,0], A[i,1], A[i,2], A[i,3],i+1); Sum language= Sum language + a[i,0]; Sum Math= Sum math + a[i,1]; Sum English= Sum English + a[i,2]; } avg Language= Sum language/6; Avg Math= Sum Math/6; Avg English= Sum English/6; Console.WriteLine ("Average score: language {0}\t Math {1}\t english {2}", AVG language, Avg math, avg English);

Two. Jagged data, array of arrays.

Defined:

First step: Define a large Array

data type [] A = new data type [number of rows] [];

Step two: Define the decimal group

Data type [] a1 = new data type [number of columns];

Data type [] a2 = new data type [number of columns];

......

Step three: Put the decimal group in a large array

A[0] = A1;

A[1] = A2;

....

Code:

     int[] A =New int[3][]; int[] a0 =New int[] {1,2,3,4,5 }; int[] A1 =New int[] {6,7,8 }; int[] A2 =New int[] {9,Ten }; a[0] =A0; a[1] =A1; a[2] =A2; //Show             for(inti =0; i < a.length; i++)//a.length=3            {                 for(intj =0; J < A[i]. Length; J + +) {Console.Write (A[i][j]+"\ t");            } Console.WriteLine (); }

★ Expand

int[,] a = new int [3][4]; Wrong

Int[][] A = new int[3,4]; Wrong

Int[][] A = new int[3][4]; Wrong

int[,] C = new int[3,4]; Right

c.length== 12;

Three. Collection

1, ArrayList linked list, no length limit, you can add or remove elements at any time .

Need to be preceded by: using System.Collections;

Defined:

ArrayList a = new ArrayList ();

Operation:

A.add (data): add

A.insert (index number, data): Insert

A.removeat (index number): delete

A.count the number of elements in the collection

Value:

a[Subscript]

The value taken out needs to be cast.

Code examples

ArrayList A =NewArrayList (); A.add ( the); A.add ( -); A.add ( -); A.add ("glutinous Rice"); A.add (true ); A.insert (1,"Chihuo");//Insert 2nd "Chihuo"A.removeat (3);//Delete line Fourth             for(inti =0; I < a.count;i++) {Console.WriteLine (a[i]); }

Results show

2, list< type > linked list, no length limit, you can add or remove elements at any time. Only data of the specified type can be placed, and no casts are taken out.

Defined

list< type > variable name = new list< type > ();

List<int> a = new list<int> ();

Operation:

A.add (data): add

A.insert (index number, data): Insert

A.removeat (index number): delete

A.count the number of elements in the collection

A.sort (); Sort

A.reverse (); reverse

Take value

a[index number]

Example:

List <int> a=Newlist<int> (); A.add (Ten); A.add ( -); A.add ( +); A.add ( -); A.insert (2,123); A.removeat (3);  for(inti =0; I < a.count;i++) {Console.WriteLine (a[i]); }        

Results show

Collection in the collection:

list<list<int>> A =Newlist<list<int>>(); List<int> a1 =Newlist<int>(); A1. ADD (Ten); A1. ADD ( A); A1. ADD ( the); List<int> a2 =Newlist<int>(); A2. ADD ( -); A2. ADD ( at); A2. ADD ( -);            A.add (A1); A.add (A2);

3.dictionary<key,value> Dictionary or hash table

Defined

Dictionary<int,string> a = new dictionary<int,string> ();

Operation:

A.add (key value, data);

A.remove (key value);

A.count;

Value:

a[Key value]

code example

4, Stack, queue

Stack: Advanced after, can not randomly take any one of the values.

stack< data type > A = new stack< data type > ();

A.push (value);

Data type variable name = A.pop ();

Team Example: FIFO, can not randomly take any one of the values.

Queue<int> a = new queue<int> ();

A.enqueue (value);

Data type variable = A.dequeue ();

201,604,232-D arrays, jagged arrays, and collections

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.