C#相邻两数组元素同步长交换

痛定思痛。 2022-12-05 12:10 225阅读 0赞
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace OOP
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] chIndex = new int[] { 1, 2, 3, 4, 5, 6 };
  11. for (int i = 0; i <= 4; i += 2)
  12. {
  13. int temp = chIndex[i];
  14. chIndex[i] = chIndex[i + 1];
  15. chIndex[i + 1] = temp;
  16. }
  17. for (int i = 0; i < chIndex.Length; i++)
  18. {
  19. Console.Write(chIndex[i] + "\t");
  20. }
  21. Console.ReadKey();
  22. }
  23. }
  24. }

运行示例:
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,225人围观)

还没有评论,来说两句吧...

相关阅读

    相关 数组指定元素交换

    (1)定义一个int类型的一维数组,内容为\{6,2,9,15,1,5,20,7,18\} (2)将数组最大元素与最后一位元素进行交换,最小元素与第一位元素进行交换,并打印