Two-dimensional arrays

Source: Internet
Author: User
Tags array length

Two-dimensional arrays:
One-dimensional array---beans
Two-dimensional array---table

Defined:
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 ...};

Two-dimensional arrays:
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]

Example: 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.


int[,] A = new int[6, 5];

for (int i = 0; i < 6;i++)
{console.write ("Please enter the language score for the {0} students:", i+1);
int yw = Convert.ToInt32 (console.readl INE ());
Console.Write ("Please enter the math score for the {0} students:", i+1);
int sx = Convert.ToInt32 (Console.ReadLine ());
Console.Write ("Please enter a foreign language score for the {0} students:", i+1);
int wy = Convert.ToInt32 (Console.ReadLine ());
A[i, 0] = i + 1;
A[i, 1] = yw;
A[i, 2] = SX;
A[i, 3] = WY;
A[i, 4] = yw + SX + WY;

}
int yp = 0; int sp = 0; int wp = 0;
for (int i = 0; I <= 5;i++)
{
for (int j = 1; J <= 5
-i;j++)
{
if (a[i, 4] > A[j- 1, 4])
{
int txh = a[j-1, 0];
a[j-1, 0] = a[j, 0];
a[j, 0] = TXH;
int tyw = a[j-1, 1];
A[j-1, 1] = a[j, 1];
int TSX = A[J-1, 2];
A[j-1, 2] = a[j, 2];
A[j, 2] = TSX;
int twy = a[j-1, 3];
A[j-1, 3] = a[j, 3];
A[j, 3] = Twy;
int tzf = a[j-1, 4];
A[j-1, 4] = a[j, 4];
A[j, 4] = TZF;



}


}

for (i = 0; i < 5; i++)
{
YP = YP + a[i, 1];
SP = sp + a[i, 2];
WP = WP + A[I, 3];

}
Double AVG1 = 1.0 * YP/3;
Double AVG2 = 1.0 * SP/3;
Double AVG3 = 1.0 * WP/3;
Console.WriteLine ("Average score \t{0}\t{1}\t{2}", Avg1, Avg2, AVG3);
}

Console.WriteLine ("Study number \ t language \ t math \ t foreign language \ T" T-rank);

for (int i = 0; i < 6;i++)
{Console.WriteLine ("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", a[i,0],a[i,1],a[i,2],a[i,3],a[i,4],i+1);}

Jagged data, an 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;
....

int[,] a = new int [3][4]; Wrong
Int[][] A = new int[3,4];//error
Int[][] A = new int[3][4];//error
int[,] C = new int[3,4];//pair

C.length???? 12;

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

int[] A1 = new int[] {3, 4, 5, 6, 7};
int[] A2 = new int[] {1, 2, 3};
int[] a3 = new int[] {7, 8, 9, 10};

A[0] = A1;
A[1] = A2;
A[2] = A3;
The above level two array of a. Length???? 3;

Collection:

One, ArrayList linked list, there is 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.

Second, list< type > Linked list, there is 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]

Third, 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]


Four, stack, the queue know it's OK
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 ();

Two-dimensional arrays

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.