C#實(shí)現(xiàn)向數(shù)組指定索引位置插入新的元素值
一、使用的方法
1.自定義插入方法
首先需要定義一個(gè)一維數(shù)組,然后修改數(shù)組的長(zhǎng)度(這里使用Length屬性獲取數(shù)組的長(zhǎng)度,然后加1,作為新數(shù)組的長(zhǎng)度),從而在其中增加一個(gè)元素。只有增加了數(shù)組的長(zhǎng)度以后才能在這個(gè)數(shù)組中增加新的元素。
2.使用List<T>.Add(T) 方法
首先,創(chuàng)建一個(gè)與原始數(shù)組大小相同的動(dòng)態(tài)數(shù)組(例如,List<T>)。然后,將原始數(shù)組的元素復(fù)制到動(dòng)態(tài)數(shù)組中,直到到達(dá)要插入元素的索引位置。在動(dòng)態(tài)數(shù)組中插入新元素。將原始數(shù)組中剩余的元素復(fù)制到動(dòng)態(tài)數(shù)組中。最后,將動(dòng)態(tài)數(shù)組轉(zhuǎn)換回?cái)?shù)組并返回。
二、實(shí)例
1.示例1:List<T>.Add(T) 方法
// 將一個(gè)元素插入到現(xiàn)有數(shù)組的指定索引位置, // 并將原來位于該位置的元素向后移動(dòng) namespace _095_1 { class Program { static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); int[] originalArray = [1, 2, 3, 4, 5]; int elementToInsert = 10; int indexToInsert = 2; int[] newArray = InsertElement(originalArray, elementToInsert, indexToInsert); Console.WriteLine(string.Join(", ", newArray)); } static int[] InsertElement(int[] originalArray, int elementToInsert, int indexToInsert) { List<int> dynamicArray = new(originalArray.Length); // Copy elements from original array to dynamic array until the insertion index for (int i = 0; i < indexToInsert; i++) { dynamicArray.Add(originalArray[i]); } // Insert the element at the specified index dynamicArray.Add(elementToInsert); // Copy remaining elements from original array to dynamic array for (int i = indexToInsert; i < originalArray.Length; i++) { dynamicArray.Add(originalArray[i]); } // Convert dynamic array to a new array and return return [.. dynamicArray]; } } } //運(yùn)行結(jié)果: /* 1, 2, 10, 3, 4, 5 */
2.示例:自定義插入方法
//在既有數(shù)組中的指定位置插入一個(gè)新的元素, //并遍歷輸出新數(shù)組 namespace _095 { public partial class Form1 : Form { private Button? button1; private Button? button2; private Label? label1; private Label? label2; private TextBox? textBox1; private TextBox? textBox2; private RichTextBox? richTextBox1; private Label? label3; private TextBox? textBox3; private int[] int_array = new int[8]; public Form1() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // button1 // button1 = new Button { Location = new Point(12, 9), Name = "button1", Size = new Size(75, 23), TabIndex = 0, Text = "生成數(shù)組", UseVisualStyleBackColor = true }; button1.Click += Button1_Click; // // button2 // button2 = new Button { Location = new Point(224, 36), Name = "button2", Size = new Size(43, 23), TabIndex = 1, Text = "確定", UseVisualStyleBackColor = true }; button2.Click += Button2_Click; // // label1 // label1 = new Label { AutoSize = true, Location = new Point(12, 42), Name = "label1", Size = new Size(56, 17), TabIndex = 2, Text = "插入索引" }; // // label2 // label2 = new Label { AutoSize = true, Location = new Point(12, 69), Name = "label2", Size = new Size(56, 17), TabIndex = 3, Text = "新數(shù)組:" }; // // textBox1 // textBox1 = new TextBox { Location = new Point(93, 9), Name = "textBox1", Size = new Size(174, 23), TabIndex = 4 }; // // textBox2 // textBox2 = new TextBox { Location = new Point(73, 36), Name = "textBox2", Size = new Size(40, 23), TabIndex = 5 }; // // richTextBox1 // richTextBox1 = new RichTextBox { Location = new Point(12, 90), Name = "richTextBox1", Size = new Size(254, 44), TabIndex = 6, Text = "" }; // // label3 // label3 = new Label { AutoSize = true, Location = new Point(118, 42), Name = "label3", Size = new Size(56, 17), TabIndex = 7, Text = "插入元素" }; // // textBox3 // textBox3 = new TextBox { Location = new Point(179, 36), Name = "textBox3", Size = new Size(40, 23), TabIndex = 8 }; // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(279, 146); Controls.Add(textBox3); Controls.Add(label3); Controls.Add(richTextBox1); Controls.Add(textBox2); Controls.Add(textBox1); Controls.Add(label2); Controls.Add(label1); Controls.Add(button2); Controls.Add(button1); Name = "Form1"; Text = "在數(shù)組中添加一個(gè)元素"; } /// <summary> /// 生成數(shù)組事件 /// 遍歷生成整形數(shù)組,并遍歷輸出 /// </summary> private void Button1_Click(object? sender, EventArgs e) { textBox1!.Clear(); for (int i = 0; i < int_array.GetUpperBound(0) + 1; i++) { int_array[i] = i; } for (int i = 0; i < int_array.GetUpperBound(0) + 1; i++) { textBox1.Text += int_array[i] + " "; } } /// <summary> /// 確定插入事件 /// 在生成的數(shù)組索引=4的位置插入一個(gè)元素,并遍歷輸出 /// 這個(gè)事件不僅調(diào)用AddArray方法,更是在調(diào)用該方法之后改變了數(shù)組的大小 /// </summary> private void Button2_Click(object? sender, EventArgs e) { richTextBox1!.Clear(); if ((textBox2!.Text != "")&& (textBox3!.Text !="")&& (textBox3!.Text.Length == 1)) { int_array = AddArray(int_array, Convert.ToInt32(textBox2!.Text), Convert.ToInt32(textBox3!.Text)); for (int i = 0; i < int_array.GetUpperBound(0) + 1; i++) { richTextBox1.Text += int_array[i] + " "; } } else { MessageBox.Show("輸入信息不能為空且元素長(zhǎng)度恒為1", "提示"); } } /// <summary> /// 向數(shù)組中插入單個(gè)元素的方法 /// </summary> /// <param name="ArrayBorn">要向其中添加元素的一維數(shù)組</param> /// <param name="Index">添加索引</param> /// <param name="Value">添加值</param> /// <returns></returns> static int[] AddArray(int[] ArrayBorn, int Index, int Value) { if (Index >= ArrayBorn.Length) Index = ArrayBorn.Length - 1; int[] TemArray = new int[ArrayBorn.Length + 1];//聲明一個(gè)新的數(shù)組 for (int i = 0; i < TemArray.Length; i++) { if (Index >= 0) { if (i < (Index + 1)) //判斷遍歷到的索引是否小于添加索引加1 TemArray[i] = ArrayBorn[i]; else if (i == (Index + 1))//判斷遍歷到的索引是否等于添加索引加1 TemArray[i] = Value; else TemArray[i] = ArrayBorn[i - 1]; } else { if (i == 0)//數(shù)組首位置 TemArray[i] = Value; else TemArray[i] = ArrayBorn[i - 1]; } } return TemArray; } } }
到此這篇關(guān)于C#實(shí)現(xiàn)向數(shù)組指定索引位置插入新的元素值的文章就介紹到這了,更多相關(guān)C#向數(shù)組插入元素值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#常用數(shù)據(jù)結(jié)構(gòu)和算法總結(jié)
這篇文章主要介紹了C#常用數(shù)據(jù)結(jié)構(gòu)和算法,這里我們總結(jié)了一些知識(shí)點(diǎn),可以幫助大家理解這些概念。2016-06-06c# 用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入
這篇文章主要介紹了c# 用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入的步驟,幫助大家更好的理解和使用c#中的Dictionary類,感興趣的朋友可以了解下2021-02-02關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會(huì)不會(huì)拋出異常詳解
這篇文章主要給大家介紹了關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會(huì)不會(huì)拋出異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04C#創(chuàng)建壓縮文件的實(shí)現(xiàn)代碼
本篇文章主要介紹了C# 創(chuàng)建壓縮文件的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05