Table of Contents
集合概述
ArrayList
语法:
ArrayList的常用方法和属性
HashTable
HashTable的常用属性和方法
泛型和泛型集合
泛型
泛型集合
泛型类
Home php教程 php手册 使用集合组织相关数据

使用集合组织相关数据

Jul 06, 2016 pm 01:30 PM
use data array Overview Related Simple organize gather

集合概述 集合简单的说就是数组的升级版。他可以动态的对集合的长度(也就是集合内最大元素的个数)进行定义和维护! ArrayList ArrayList非常类似于数组。也有人称他为数组列表,它的容量可以根据需要进行动态扩充,而它的索引也会根据集合容量的扩展而重新

集合概述

集合简单的说就是数组的升级版。他可以动态的对集合的长度(也就是集合内最大元素的个数)进行定义和维护!

ArrayList

ArrayList非常类似于数组。也有人称他为数组列表,它的容量可以根据需要进行动态扩充,而它的索引也会根据集合容量的扩展而重新分配和调整。也就是说ArrayList集合中的元素的下标是不确定的、可变的。

ArrayList类属于System.Collections命名空间,这个命名空间包含接口和类,这些接口和类定义各种对象(如列表、队列、位数组、哈希表和字典)的集合。

语法:

Using System.Collections;<span style="color: #008000">//</span><span style="color: #008000">导入命名空间
</span><span style="color: #008000">//</span><span style="color: #008000">定义ArrayList对象</span>
ArrayList    ArrayList集合名称=<span style="color: #0000ff">new</span>  ArrayList(【长度】);
Copy after login

ArrayList的常用方法和属性

               属性名称                说明
              Count 获取ArrayList中实际包含的元素数
      返回值类型         方法名称         说明
           int   add(Object Value) 将对象添加到ArrayList的结尾处
           Void   RemoveAt(int  index) 移除ArrayList指定索引处的元素
           Void   Remove(Object  Value) 从ArrayList中移除特定的对象
           Void   Clear() 从ArrayList中移除所有的元素

HashTable

在ArrayList中我们可以通过索引访问集合中的元素,但是当集合内的元素索引频繁变化时要找出每个元素的位置(索引)就变得非常麻烦你了,

C#提供了一种叫HashTable的数据结构,通常称他为哈希表也有人称他为“字典”,给它起字典这个名字是因为它和字典非常类似,都是通过一个单词查找出有关此单词的更多信息,HashTable是通过键(Key)对值(Value)来组织数据的

HashTable的常用属性和方法

                属性                       说明
count 获取包含在HashTable中键值对的数目
Keys 获取包含HasTable中键的集合
Values 获取包含HasTable中值得集合

  返回值类型 方法名称                             说明
void Add(Object Key,Object Value) 将带有指定键和值得元素添加到HashTable中
Void Remove(Object  Key) 从HashTable中移除带有特定键的元素
Void Clear() 清除HashTable中的所有元素
泛型和泛型集合

泛型和泛型集合

通过ArrayList和HashTable存储的数据都会转换成Object类型,这就意味着它可以在一个集合中存储不同的类型元素,在遍历集合时就有可能引发强制类型转换错误的问题。而泛型集合是类型安全的,在定义时就先定了集合中的元素类型,下面将详细讲解泛型和泛型集合。

泛型

泛型是C#2.0 中的一个新特性,泛型引入了一个概念:类型参数,通过使用类型参数(T)减少了运行时强制类型转换或装箱拆箱操作的风险,同过泛型可以最大限度的重用代码,保护类型安全及提高性能,它的最常见应用是创建集合类,可以约束集合类中的元素类型。个比较典型的泛型集合是LIst和Dictionary,下面进行详细讲解、

泛型集合

在System.Collections.Generic命名空间中定义了去多泛型集合类这些类可以代替上面的ArrayList

定义一个List集合的语法如下:

List   集合名称=new   List();

”中的T可以对集合中的元素类型进行约束,T表名集合管理的元素类型。

List和ArrayList的区别

  1. List对保存的元素类型做约束,ArrayList可以保存任何类型的元素
  2. list保存值类型元素不会进行装箱拆箱操作而ArrayList会

泛型集合Dictionary

泛型集合Dictionary可以代替上面的HashTable

定义一个泛型集合Dictionary的语法

Dictionary  集合名称=new   Dictionary();

在””中K表示集合中Key的类型,V表示Value的类型,他们的含义和List是相同的

    1. Dictionary对保存的元素类型做约束,HashTablet可以保存任何类型的元素
    2. Dictionary保存值类型元素不会进行装箱拆箱操作而HashTablet会

泛型类

在集合中使用泛型只是泛型多种应用的一种,在类、方法等方面已有泛型的应用

定义一个泛型类的语法如下:

<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span>     类名<t><span style="color: #000000">
{
    </span><span style="color: #008000">//</span><span style="color: #008000">....</span>
}</t>
Copy after login

T指类型参数,代表具体的数据类型,可以是值类型也可以是引用类型


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

I cry to death. The world is madly building big models. The data on the Internet is not enough. It is not enough at all. The training model looks like "The Hunger Games", and AI researchers around the world are worrying about how to feed these data voracious eaters. This problem is particularly prominent in multi-modal tasks. At a time when nothing could be done, a start-up team from the Department of Renmin University of China used its own new model to become the first in China to make "model-generated data feed itself" a reality. Moreover, it is a two-pronged approach on the understanding side and the generation side. Both sides can generate high-quality, multi-modal new data and provide data feedback to the model itself. What is a model? Awaker 1.0, a large multi-modal model that just appeared on the Zhongguancun Forum. Who is the team? Sophon engine. Founded by Gao Yizhao, a doctoral student at Renmin University’s Hillhouse School of Artificial Intelligence.

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! May 06, 2024 pm 04:13 PM

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Single card running Llama 70B is faster than dual card, Microsoft forced FP6 into A100 | Open source Apr 29, 2024 pm 04:55 PM

FP8 and lower floating point quantification precision are no longer the "patent" of H100! Lao Huang wanted everyone to use INT8/INT4, and the Microsoft DeepSpeed ​​team started running FP6 on A100 without official support from NVIDIA. Test results show that the new method TC-FPx's FP6 quantization on A100 is close to or occasionally faster than INT4, and has higher accuracy than the latter. On top of this, there is also end-to-end large model support, which has been open sourced and integrated into deep learning inference frameworks such as DeepSpeed. This result also has an immediate effect on accelerating large models - under this framework, using a single card to run Llama, the throughput is 2.65 times higher than that of dual cards. one

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

See all articles