最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現向數組指定索引位置插入新的元素值

 更新時間:2024年02月07日 10:48:22   作者:wenchm  
這篇文章給大家介紹了利用C#實現向數組指定索引位置插入新的元素值,首先需要定義一個一維數組,然后修改數組的長度,從而在其中增加一個元素,需要的朋友可以參考下

一、使用的方法

1.自定義插入方法 

首先需要定義一個一維數組,然后修改數組的長度(這里使用Length屬性獲取數組的長度,然后加1,作為新數組的長度),從而在其中增加一個元素。只有增加了數組的長度以后才能在這個數組中增加新的元素。

2.使用List<T>.Add(T) 方法

首先,創(chuàng)建一個與原始數組大小相同的動態(tài)數組(例如,List<T>)。然后,將原始數組的元素復制到動態(tài)數組中,直到到達要插入元素的索引位置。在動態(tài)數組中插入新元素。將原始數組中剩余的元素復制到動態(tài)數組中。最后,將動態(tài)數組轉換回數組并返回。

二、實例

1.示例1:List<T>.Add(T) 方法

// 將一個元素插入到現有數組的指定索引位置,
// 并將原來位于該位置的元素向后移動
 
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];
        }
    }
}
//運行結果:
/*
1, 2, 10, 3, 4, 5
 */

2.示例:自定義插入方法

//在既有數組中的指定位置插入一個新的元素,
//并遍歷輸出新數組
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 = "生成數組",
                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 = "新數組:"
            };
            // 
            // 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 = "在數組中添加一個元素";
 
        }
        /// <summary>
        /// 生成數組事件
        /// 遍歷生成整形數組,并遍歷輸出
        /// </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>
        /// 確定插入事件
        /// 在生成的數組索引=4的位置插入一個元素,并遍歷輸出
        /// 這個事件不僅調用AddArray方法,更是在調用該方法之后改變了數組的大小
        /// </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("輸入信息不能為空且元素長度恒為1", "提示");
            }
        }
 
        /// <summary>
        /// 向數組中插入單個元素的方法
        /// </summary>
        /// <param name="ArrayBorn">要向其中添加元素的一維數組</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];//聲明一個新的數組
            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)//數組首位置
                        TemArray[i] = Value;
                    else
                        TemArray[i] = ArrayBorn[i - 1];
                }
            }
            return TemArray;
        }
    }
}

到此這篇關于C#實現向數組指定索引位置插入新的元素值的文章就介紹到這了,更多相關C#向數組插入元素值內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C#中的multipart/form-data提交文件和參數

    C#中的multipart/form-data提交文件和參數

    這篇文章主要介紹了C#中的multipart/form-data提交文件和參數,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • C#獲取動態(tài)生成的CheckBox值

    C#獲取動態(tài)生成的CheckBox值

    checkbox是VS2012的常用控件之一,可以方便的為某些功能取消或啟用,下面教你如何簡單使用checkbox。本文通過兩種方法給大家介紹,需要的朋友一起看看吧
    2015-09-09
  • C#結合JS解決Word添加無效位圖導致進程停滯的問題

    C#結合JS解決Word添加無效位圖導致進程停滯的問題

    這篇文章主要為大家詳細介紹了C#如何結合JS解決Word添加無效位圖導致進程停滯的問題,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-11-11
  • C#委托與匿名委托詳解

    C#委托與匿名委托詳解

    這篇文章主要為大家詳細介紹了C#委托與匿名委托的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C#利用反射實現多數據庫訪問

    C#利用反射實現多數據庫訪問

    本文詳細講解了C#利用反射實現多數據庫訪問的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • c#使用EPPlus將圖片流嵌入到Excel實現示例

    c#使用EPPlus將圖片流嵌入到Excel實現示例

    這篇文章主要為大家介紹了c#使用EPPlus將圖片流嵌入到Excel實現示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-12-12
  • C#導出Excel的示例詳解

    C#導出Excel的示例詳解

    這篇文章主要為大家詳細介紹了C#導出Excel的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C#模擬Http與Https請求框架類實例

    C#模擬Http與Https請求框架類實例

    這篇文章主要介紹了C#模擬Http與Https請求框架類,實例分析了處理http與https請求的方法與信息處理的技巧,需要的朋友可以參考下
    2014-12-12
  • C#實現高效讀寫Excel工作表

    C#實現高效讀寫Excel工作表

    Excel 是各行業(yè)數據管理的核心載體,廣泛應用于財務統(tǒng)計、庫存管理、報表生成等場景,本文主要介紹了C#如何借助免費庫 Free Spire.XLS for .NET實現讀寫 Excel 工作表,有需要的可以了解下
    2025-10-10
  • C#中using的使用方式詳解

    C#中using的使用方式詳解

    這篇文章主要介紹了C#中using的使用方式方式詳解,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06

最新評論

巨野县| 平阳县| 吉水县| 五原县| 建湖县| 桃园县| 永清县| 江门市| 溧水县| 望谟县| 益阳市| 澄城县| 开封县| 越西县| 秀山| 阳江市| 泸西县| 逊克县| 平定县| 吉安县| 贺州市| 甘孜县| 黑水县| 合山市| 平湖市| 东城区| 新巴尔虎右旗| 牡丹江市| 彰化市| 康乐县| 平武县| 德清县| 察哈| 大埔县| 奉新县| 平定县| 茂名市| 怀柔区| 宁德市| 宁河县| 新干县|