调整数组[,]或数组[] []的大小 [英] Resize an array [,] or an array [][]

查看:91
本文介绍了调整数组[,]或数组[] []的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

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

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


这篇关于调整数组[,]或数组[] []的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆