首页 > 其他分享 >数组能改变大小吗

数组能改变大小吗

时间:2023-09-28 16:26:08浏览次数:52  
标签:Console WriteLine 改变 values 数组 大小 array myArr string

提问

数组能改变大小吗

回答

using System;

public class SamplesArray
{
    public static void Main()  {

        // Create and initialize a new string array.
        String[] myArr = {"The", "quick", "brown", "fox", "jumps",
            "over", "the", "lazy", "dog"};

        // Display the values of the array.
        Console.WriteLine(
            "The string array initially contains the following values:");
        PrintIndexAndValues(myArr);

        // Resize the array to a bigger size (five elements larger).
        Array.Resize(ref myArr, myArr.Length + 5);

        // Display the values of the array.
        Console.WriteLine("After resizing to a larger size, ");
        Console.WriteLine("the string array contains the following values:");
        PrintIndexAndValues(myArr);

        // Resize the array to a smaller size (four elements).
        Array.Resize(ref myArr, 4);

        // Display the values of the array.
        Console.WriteLine("After resizing to a smaller size, ");
        Console.WriteLine("the string array contains the following values:");
        PrintIndexAndValues(myArr);
    }

    public static void PrintIndexAndValues(String[] myArr)  {
        for(int i = 0; i < myArr.Length; i++)
        {
            Console.WriteLine("   [{0}] : {1}", i, myArr[i]);
        }
        Console.WriteLine();
    }
}

/*
This code produces the following output.

The string array initially contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox
   [4] : jumps
   [5] : over
   [6] : the
   [7] : lazy
   [8] : dog

After resizing to a larger size,
the string array contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox
   [4] : jumps
   [5] : over
   [6] : the
   [7] : lazy
   [8] : dog
   [9] :
   [10] :
   [11] :
   [12] :
   [13] :

After resizing to a smaller size,
the string array contains the following values:
   [0] : The
   [1] : quick
   [2] : brown
   [3] : fox

*/

参考

https://learn.microsoft.com/zh-cn/dotnet/api/system.array.resize?view=net-6.0

标签:Console,WriteLine,改变,values,数组,大小,array,myArr,string
From: https://www.cnblogs.com/wuhailong/p/17736018.html

相关文章

  • 山海鲸数字孪生金融解决方案:智能化改变金融业
    在金融行业的数字化浪潮中,数字孪生技术正以惊人的速度崭露头角。这项技术不仅改变了金融机构的运营方式,还为金融从业者提供了更多机会来提高效率、降低风险以及提供更加个性化的服务。为了解决这一需求,山海鲸可视化退出一系列智慧金融解决方案案例,下面带大家了解一下山海鲸可视化......
  • 如何将数组中元素为空的数据过滤掉?
    场景:后台返回所有文件列表信息,需要将fileId有值的文件过滤出来回显到页面上。错误处理:使用map+if判断letarr=[{fileId:'1',fileName:'缴费明细表'},{fileId:'2',fileName:'支付明细表'},{fileId......
  • 根据一个数组,创建一个Segment Tree(线段树)
    线段树的特点线段树的优势线段树的构造过程(0,5)37:数组元素下标0~5的元素之和是37(0,2)21:数组元素下标0~2的元素之和是21线段树的基本数据结构(结点结构由五个分量组成)运行结果(C语言代码)递归的创建一颗线段树,然后中序、先序、后序遍历这个结点#include<stdio.h>#include<st......
  • 案例8:将"picK"的大小写互换
    最终输出结果为PICk。需要先计算两个字母之间的间隔,比如a和A之间的间隔为多少。然后在将大写字母转换为小写字母,加上间隔的值;将小写字母转换为大写字母,减去间隔的值。示例代码如下:#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>voidmain(){ charc1='p',c2......
  • js 检索数组对象中某个属性的值是否不相等
    //判断选中的项是否有不相同的模型consthasNameProperty=_this.selectedOrderList.filter(order=>order.hasOwnProperty("item_model_id"));if(hasNameProperty.length>0){constisEveryNameEqual=hasNameProperty.reduce((prev,curr)=>{if......
  • python numpy 数组操作
          ......
  • vue 数组删除(对象)单/多条删除
    dataList:[{id:'1',value:'aaaa',},{id:'2',value:'bbb',},{id:'3',value:'ccc',},{......
  • 2023-09-16:用go语言,给你一个整数 n 和一个在范围 [0, n - 1] 以内的整数 p , 它们表示
    2023-09-16:用go语言,给你一个整数n和一个在范围[0,n-1]以内的整数p,它们表示一个长度为n且下标从0开始的数组arr,数组中除了下标为p处是1以外,其他所有数都是0。同时给你一个整数数组banned,它包含数组中的一些位置。banned中第i个位置表示arr[banned[i]]=......
  • Mysql查询不区分大小写
    当使用mysql查询时,发现不区分大小写。经查,是以下问题utf8_general_ci --不区分大小写utf8_bin--区分大小写  解决方法一:修改排序规则为utf8_bin 解决方法二:在查询的字段前面加个binary ......
  • socket 编程发送和接收缓存大小匹配问题
    socket编程中有发送、接收缓存的情况:例如服务器向客户端发送:105charserver_buf[BUF_SIZE]={0};123if(strncmp(server_buf,"exit",4)!=0)124{125printf("Servergot:%s\n",server_buf);126//将得到的字符串传给......