首页 > 其他分享 >二维数组的简单用法

二维数组的简单用法

时间:2024-10-17 22:59:10浏览次数:1  
标签:ToArray int Array2D 用法 二维 static 数组 Enumerable public

    public class IntArrayDemo
    {
        public static void Print()
        {
            for (int i = 0; i<IntArray.Ints.Length; i++) 
            {
                Console.WriteLine(i);
            }
        }

        public static void GetValue() 
        {
            var First = IntArray.GetFirstRow();
            foreach (int i in First)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("----------------------");
            var Second = IntArray.GetSecondRow();
            foreach (int i in Second)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("----------------------");
            var Third=IntArray.GetThirdRow();
            foreach (int i in Third)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("----------------------");
            int[] firstColumn = IntArray.GetFirstColumn();
            foreach (int i in firstColumn)
            {
                Console.WriteLine(i);
            }
        }
    }

    public class IntArray
    {
        //报错MissingMemberException: The lazily-initialized type does not have a public, parameterless constructor.
        //可以看出执行顺序
      /*  private static Lazy<int[]> ints=new Lazy<int[]>();
        public static int[] Ints=ints.Value;
        static IntArray()
        {

            Ints = Enumerable.Range(0,9).ToArray();
        }
*/
        private static Lazy<int[]> ints = new Lazy<int[]>(() => Enumerable.Range(0, 9).ToArray());
        public static int[] Ints => ints.Value;

        //二维数组

        public static int[,] Array2D = new int[3, 4] {
            { 1, 2, 3, 4 },
            { 5, 6, 7, 8 },
            { 9, 10, 11, 12 }
            };
        // 获取第一行数据
        public static int[] GetFirstRow()
        {
            return Enumerable.Range(0, Array2D.GetLength(1))
                .Select(col => Array2D[0, col])
                .ToArray();
        }

        // 获取第二行数据
        public static int[] GetSecondRow()
        {
            return Enumerable.Range(0, Array2D.GetLength(1))
                .Select(col => Array2D[1, col])
                .ToArray();
        }

        // 获取第三行数据
        public static int[] GetThirdRow()
        {
            return Enumerable.Range(0, Array2D.GetLength(1))
                .Select(col => Array2D[2, col])
                .ToArray();
        }

        // 获取第一列数据
        public static int[] GetFirstColumn()
        {
            return Enumerable.Range(0, Array2D.GetLength(0))
                .Select(row => Array2D[row, 0])
                .ToArray();
        }
    }

标签:ToArray,int,Array2D,用法,二维,static,数组,Enumerable,public
From: https://www.cnblogs.com/guchen33/p/18473283

相关文章

  • 力扣349.两个数组的交集
    题目链接:349.两个数组的交集-力扣(LeetCode)给定两个数组 nums1 和 nums2 ,返回 它们的 交集。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。示例1:输入:nums1=[1,2,2,1],nums2=[2,2]输出:[2]示例2:输入:nums1=[4,9,5],nums2=[......
  • 每日OJ题_牛客_连续子数组最大和_线性dp_C++_Java
    目录牛客_连续子数组最大和_线性dp题目解析C++代码Java代码牛客_连续子数组最大和_线性dp连续子数组最大和_牛客题霸_牛客网(nowcoder.com)描述:        给定一个长度为 n的数组,数组中的数为整数。请你选择一个非空连续子数组,使该子数组所有数之和尽可能大,......
  • Java 一维数组作为函数参数
    //一维数组的引用#defineSIZE5voidinput(inta[],intlen);voidoutput(inta[],intlen);//函数的声明intmain(void){   inti=0;   intarr[SIZE]={86,85,85,896,45};//同理五个数据只是偶然,可能会更多   //输入   input(arr,SIZE);......
  • LeetCode 209 - 长度最小的子数组(滑动窗口法)
    题目描述给定一个含有n个正整数的数组和一个正整数target,我们要找出该数组中满足其总和大于等于target的长度最小的子数组,即子数组[nums_left,nums_right],并返回其长度。如果不存在符合条件的子数组,返回0。解题思路我们可以使用滑动窗口解决这道问题。初始化左指针......
  • (算法)最⻓湍流⼦数组————<动态规划>
    1.题⽬链接:978.最⻓湍流⼦数组2.题⽬描述:3.解法(动态规划):算法思路:1.状态表⽰:我们先尝试定义状态表⽰为:dp[i]表⽰「以i位置为结尾的最⻓湍流数组的⻓度」。 但是,问题来了,如果状态表⽰这样定义的话,以i位置为结尾的最⻓湍流数组的⻓度我们没法从之前的状态推导出......
  • 【数据结构】之数组详解
    数组是数据结构中最基础的概念之一,它在编程中被广泛应用。理解数组的工作原理、操作方式以及底层实现,对于我们构建更复杂的数据结构和算法至关重要。本文将从多个角度深入剖析数组,并以Java语言示例进行讲解,帮助你建立对数组的深刻理解。一、什么是数组?数组是一种线性数据结构......
  • 实验三: JavaScript数组与函数
    实验目的熟练掌握常用JavsScript的数组、自定义函数、作用域。实验内容数组定义及元素获取;数组的遍历;数组内容的增删改查;数组的排序;数组的反转、截取、合并、元素拼接函数的声明;函数的调用;匿名函数;作用域。实验步骤:数组定义及元素获取;数组的遍历;数组内容的增删改查......
  • C语言[数组作函数参数]
    输入10个整数作为一个数组,要求判断并且输出其中最大的值和它是数组中的第几位数。本次代码调用max函数数组元素为a[1]~a[9]代码如下:#include<stdio.h>intmain(){  intmax(intx,inty);  inti,m,n,a[10];  printf("enter10intergernumber:"); ......
  • 代码随想录算法训练营第二天|209长度最小的子数组、59螺旋矩阵
    1leetcode209长度最小的子数组题目链接:209.长度最小的子数组文章链接:代码随想录(programmercarl.com)视频链接:拿下滑动窗口!|LeetCode209长度最小的子数组思路:没有思路,看到这道题有一种想立马退出的感觉,无从下手1.1暴力搜索1.1.1python版本这个版本的新知识就是定义......
  • Java 8 的 Lambda、函数式接口、Stream 用法和原理
    我是风筝,公众号「古时的风筝」。一个兼具深度与广度的程序员鼓励师,一个本打算写诗却写起了代码的田园码农!文章会收录在JavaNewBee中,更有Java后端知识图谱,从小白到大牛要走的路都在里面。公众号回复『666』获取高清大图。就在今年Java25周岁了,可能比在座的各位中的一些......