调整数组[,]或数组[] []的大小

编程入门 行业动态 更新时间:2024-10-27 19:23:29
本文介绍了调整数组[,]或数组[] []的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 如果我要调整数组的大小(例如):

Hello guys, If I resize an array doing this (e.g.):

String names[] = new String [1]; int numberofnames = 8; Array.Resize(ref names, numberofnames);

如何处理一维以上的数组?:

How can I do for an array with more than one dimension?:

//first column for names, second for places String namesplaces[,] = new String[1,2];

我想将数组更改为新的维度,例如[10,2]甚至[20,5];我的意思是,任意数量的行和列. 我只想要一个.NET函数,就像上面给出的"Array.Resize"一样.谢谢

I want to change the array to a new dimension such as [10,2] or even [20,5]; I mean, any number of rows and columns. I ONLY want a .NET function just like the ''Array.Resize'' given above... ¿Does it exist? Thanks

推荐答案

private void ResizeArray(ref string[,] original, int cols, int rows) { //create a new 2 dimensional array with //the size we want string[,] newArray = new string[rows, cols]; //copy the contents of the old array to the new one Array.Copy(original, newArray, original.Length); //set the original to the new array original = newArray; } // so this resizes the array to 10 columns and 2 rows.. ResizeArray(list,10,2);

抱歉:)我没有读完你的最后一句话,所以你可以使用它来回答它; System.Collections.ArrayList类,它是一个可调整自身大小的一维数组.如果您有一个ArrayList元素都是ArrayLists,它将作为多维使用,因此它将自动调整数组的大小.

hmm sorry :) I didnt read your last sentence so answer to it you may use; System.Collections.ArrayList class, which is a one-dimensional array that resizes itself. If you have an ArrayList whose elements were all ArrayLists, that will work as a multidimensional, so then it will automatically resize the array.

它不存在. 您可以针对固定维数的数组自己实现此类功能,但我没有解释如何做,因为您可能只知道并询问有关您所询问的内容.而且,这样的功能会很慢. 您可以对锯齿状数组使用常规的Array.Resize(例如string[][]),但我没有解释如何做,因为您可能只知道并询问有关您所询问的内容.出于同样的原因,我没有解释如何使用像System.Collections.Generic.List这样的集合.
—SA
It does not exist. You can implement such function yourself for array of fixed dimension, but I''m not explaining how, because you probably know and ask only about what you asked. Also, such function would be slow. You could use regular Array.Resize for jagged arrays (like string[][]), but I''m not explaining how, because you probably know and ask only about what you asked. By the same reason, I''m not explaining how to use collections like System.Collections.Generic.List.
—SA

更多推荐

调整数组[,]或数组[] []的大小

本文发布于:2023-11-30 11:14:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649772.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   大小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!