数组和集合实例

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[] arr1 = new int[2] {1,2};
int[,] arr2 = new int[2, 3] {
{0,1,2 },
{2,3,4 }
};

Console.WriteLine(arr2[1,1]);

ArrayList
ArrayList alt = new ArrayList();
alt.Add("123");
alt.Add(123);
alt.Add(true);
bool iscontain = alt.Contains("1");
alt.Clear();
/*alt.Insert(0, "abc")*/
;
Console.WriteLine(iscontain);

for(int i = 0; i < alt.Count; i++)
{
Console.WriteLine(alt[i].ToString() + " " + alt[i].GetType().ToString());
}

foreach (var x in alt)
{
Console.WriteLine(x.ToString() + " " + x.GetType().ToString());
}

泛型集合 List
List<string> str_list = new List<string>();
str_list.Add("a");
str_list.Add("b");
str_list.Add("c");
str_list.Add("d");

foreach(string x in str_list)
{
Console.WriteLine(x);
}

哈希表hashtable

Hashtable ht = new Hashtable();
ht.Add("1","a");
ht.Add(2, "b");
ht.Add(3, false);
ht.Add("x", 3.14);
Console.WriteLine(ht[2]);
foreach(var x in ht.Keys)
{
Console.WriteLine(ht[x]);
}

字典表 Dictionary

Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("a", 3);
dic.Add("b", 4);
dic.Add("c", 5);
dic.Add("d", 6);
dic.Add("e", 7);

foreach(var x in dic.Keys)
{
Console.WriteLine(x);
}

队列 queue

Queue que = new Queue();
que.Enqueue("张三");
que.Enqueue("李四");
que.Enqueue("王五");
que.Enqueue("赵六");
Console.WriteLine("现在的长度是" + que.Count);
Console.WriteLine(que.Dequeue());
Console.WriteLine("现在的长度是" + que.Count);

堆栈 stack

Stack st = new Stack();
st.Push("a");
st.Push("b");
st.Push("c");
st.Push("d");

Console.WriteLine(st.Count);
Console.WriteLine(st.Pop());
Console.WriteLine(st.Count);

Console.ReadLine();

int[] arr1 = new int[2] { 1, 2 };
int[,] arr2 = new int[2, 3]{
{0,1,2},{2,3,4}
};
Console.WriteLine(arr2[1, 1]);

ArrayList 特点不需要不需要限长度,不需要规定类型,缺点:键值只能是0,1,2,3往后排:

ArrayList alt = new ArrayList();
alt.Add("123");
alt.Add(123);
alt.Add(true);
alt.Remove(123); 从其中移除 clear 是全部清空的意思
bool iscontain=alt.Contains("123"); 看看里面是否包含"123",如果有就会显示"true",如果没有就会显示false;
alt.Clear(); 全部清除:
alt.Insert(0,"abc"); 意思是往 0 位置插入一个字符串"123";

Console.WriteLine(alt);
for (int i = 0; i < alt.Count;i++ ) {
Console.WriteLine(alt[i].ToString()+" "+alt[i].GetType().ToString());

}

var类型是 万能类型
foreach(var x in alt){

Console.WriteLine(x.ToString()+" "+alt[i].GetType().ToString)

}
泛型集合 list 需要规定类型,不需要规定长度 也不行规定键值 也是从0,1,2,3开始排

List<string> str_list = new List<string>();
str_list.Add("a");
str_list.Add("b");
str_list.Add("c");
str_list.Add("d");

foreach (string x in str_list) {

Console.WriteLine(x);

}

哈希表hashtable 不错在顺序的概念

Hashtable ht = new Hashtable();
ht.Add("1", "a");
ht.Add(2, "b");
ht.Add(3, false);
ht.Add("x", 3.14);
//Console.WriteLine(ht[2]);
foreach (var x in ht.Values) {

Console.WriteLine(x);

}
foreach (var x in ht.Keys)
{

Console.WriteLine(x); //取全部的值
Console.WriteLine(ht[x]);

}

字典表 Dictionary
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("a", 3);
dic.Add("b", 4); 前面的是key键 后面的是value值
dic.Add("c", 5);
dic.Add("d", 6);
dic.Add("e", 7);
dic.Add("f", 8);

foreach (var x in dic.Keys) {

Console.WriteLine(x);
}

队列 queue 先进先出
Queue que = new Queue();
que.Enqueue("张三");
que.Enqueue("李四");
que.Enqueue("王五");
que.Enqueue("赵六");
Console.WriteLine("现在的长度是" + que.Count);
Console.WriteLine(que.Dequeue()); //dequeue()函数用于移除每个匹配元素的指定队列中的第一个函数,并执行被移除的函数。
Console.WriteLine("现在的长度" + que.Count);

堆栈 stack 先进后出

Stack st = new Stack();
st.Push("a");
st.Push("b");
st.Push("c");
st.Push("d");
st.Push("e");

Console.WriteLine(st.Count);
Console.WriteLine(st.Pop());
Console.WriteLine(st.Count);

Console.ReadLine();

}
}
}

时间: 2024-08-28 07:51:27

数组和集合实例的相关文章

jQuery遍历对象、数组、集合实例

1.jquery 遍历对象 复制代码代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  <HTML>   <HEAD>    <TITLE> New Document </TITLE>    <script language="javascript" type="text/javascript"

FLEX 集合数组ArrayCollection操作实例

FLEX 集合数组ArrayCollection操作实例 <?xml version="1.0" encoding="utf-8"?> <!-- Simple example to demonstrate the Halo DataGrid control. --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="librar

[.net 面向对象编程基础] (17) 数组与集合

[.net 面向对象编程基础] (17) 数组与集合 学习了前面的C#三大特性,及接口,抽象类这些相对抽象的东西以后,是不是有点很累的感觉.具体的东西总是容易理解,因此我们在介绍前面抽象概念的时候,总是举的是具体的实例以加深理解. 本节内容相当具体,学起来也相当轻松. 1.数组 1.1 什么是数组? 数组是一种数据结构,包含同一个类型的多个元素. 1.2数组的初始化 string[] mystringArray; 类型+方框号 数组名 1.3数组初始化 我们知道数组是引用类型,所以需要给他分配堆

数组、集合、泛型解析——【新生入学系统】

一.概念(from 百科) 数组:把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标. 集合:集合是一组可变数量的数据项(可为0)的组合,这些数据项可能共享某些特征,需要以某种操作方式一起进行操作. 泛型:在程序编码中一些包含参数的类型,也就是说泛型的参数只可以代表类,不能代表个别对象. 二.进化史 开始用内存中的一个位置映射一个值,用"变量"来使用这个值.进一步发展成,用变量来引用一组值,数组诞生.后来有了集合,它是更强的的数组,依

C#语言中数组和集合

数组.集合→用于储存多个同类型的数据数组 定长→用于保存固定数量的数据 在功能上,数组能实现的所有功能,集合都能实现:反之,集合能实现的某些功能,数组难以实现 占用内存少 便利速度快集合 不定长→保存的数据数量,可以在程序的执行过程中,发生变化 占用内存多 便利速度慢课时六:数组和集合 数组.集合→用于储存多个同类型的数据 数组 定长→用于保存固定数量的数据 在功能上,数组能实现的所有功能,集合都能实现:反之,集合能实现的某些功能,数组难以实现 占用内存少 便利速度快 集合 不定长→保存的数据数

.NET 基础 一步步 一幕幕[数组、集合、异常捕获]

数组.集合.异常捕获 数组: 一次性存储多个相同类型的变量. 一维数组: 语法: 数组类型[] 数组名=new 数组类型[数组长度]; 声明数组的语法: A.数据类型 [] 数组名称= new 数据类型[2]{1,2}: B.数据类型 [] 数组名称 = new 数据类型[数组大小]; C. 数据类型 [] 数组名称 = {数据,数据,数据,数据}; ***数组的长度一旦固定了,就不能再被改变了 可以通过索引来访问数组中的元素: 数组名称[索引位置] 案例: 多维数组:多个线性数组的值 二维:i

非计算机专业的码农C#学习笔记 五、数组和集合

数组和集合 1.数组问题Array (1)一维数组:int[] arr={1,2,3,5,5} string[] s={s,l,s,g} (2)二维数组:int[,] arr=new int[2,2]{{1,2},{3,4}} 类型[,] 数组名=new 类型[行数(元素数),列数(元素的子元素数]{{元素1,元素2},{元素…},…,} l  动态数组:类型[,] 数组名=new 类型[M,N],int M=””;int N=””; l  查看内部元素:foreach(int n in arr

集合转数组,数组转集合,集合中只可以装引用数据类型

1 package com.pang.jihe_demo; 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.List; 6 7 public class Demo01 { 8 public static void main(String[] args) { 9 //集合转数组,集合中只可以装引用数据类型 10 ArrayList<Integer> list = new ArrayList

java中有关数组或集合的起始位详解

在jdbc连接数据库时,起始位都是从1开始的 例如:Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis", "root", "ms"); String sql="SELECT * FROM users WHERE NAME=?"; pstm=