如何添加两个动态arraylist的元素 [英] how to add elements of two dynamic arraylist

查看:88
本文介绍了如何添加两个动态arraylist的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我遇到了问题请尝试解决。在我的项目中,我需要arraylist,其大小可能相同或不同。我想添加两个arraylist的元素,即arr1 [0] + arr2 [0],arr1 [1] + arr2 [1]。

我的问题是如果两个arraylist的大小不同那么如何添加超出索引的元素,即如果arr1有三个元素,arr2有四个元素,那么arr1和arr2的第三个元素它工作正常但是在arr1的第四个索引不存在则显示错误....

谢谢

Hi,
I am stuck with a problem pls try to solve. In my project i have to arraylist whose size may or may be not same. I want to add elements of the two arraylist i.e arr1[0]+arr2[0],arr1[1]+arr2[1].
My problem is if the size of two arraylist is different then how it add the elements which is out of index i.e if arr1 has three elements and arr2 has four elements then upto third element of arr1 and arr2 it works fine but at the fourth index of arr1 which does not exist is shows error....
thanks

推荐答案

int length1 = arr1.Count;

int length2 = arr2.Count;

int minLength =(length1< length2)?length1:length2;

int maxLength =(length1> length2)?length1:length2;

控制台。 WriteLine(结果数组列表是:);

for(int i = 0; i< minLength; i ++)

{

Console.WriteLine(Convert.ToInt32(arr1 [i])+ Convert.ToInt32(arr2 [i]));

}



if(minLength!= maxLength)

{

if(arr1.Count == maxLength)

{

int i = minLength;

while(i!= maxLength)

{

Console.WriteLine(Convert.ToInt32(arr1 [i]));

i ++;

}

}



if(arr2.Count == maxLength)

{

int i = minLength;

while(i!= maxLength)

{

Console.WriteLine(Convert.ToInt32(arr2 [i]));

i ++;

}

}

}

Console.ReadLine();



注意:ArrayList是Object所以不要忘记转换到相应的类型
int length1 = arr1.Count;
int length2 = arr2.Count;
int minLength= (length1<length2)?length1:length2;
int maxLength= (length1>length2)?length1:length2;
Console.WriteLine("The Resulted Array List Are:");
for(int i=0; i < minLength; i++)
{
Console.WriteLine(Convert.ToInt32(arr1[i])+Convert.ToInt32(arr2[i]));
}

if (minLength != maxLength)
{
if (arr1.Count == maxLength)
{
int i = minLength;
while (i != maxLength)
{
Console.WriteLine(Convert.ToInt32(arr1[i]));
i++;
}
}

if (arr2.Count == maxLength)
{
int i = minLength;
while (i != maxLength)
{
Console.WriteLine(Convert.ToInt32(arr2[i]));
i++;
}
}
}
Console.ReadLine();

Note: ArrayList is Object so dont forget to convert into respective type


//声明第一个数组列表和两个项目

ArrayList list = new ArrayList();

//你可以在这里动态添加你的列表它是manualy为demo purpuse添加

list.Add(1);

list.Add(2);

// //声明第二个数组列表和4个项目

ArrayList list2 = new ArrayList();

list2.Add(1);

list2.Add(2);

list2.Add(3);

list2.Add(4);

// if codition

if(list.Count> list2.Count)

{

for(int i = 0; i< list2.Count; i ++)

{

Response.Write((int)list [i] +(int)list2 [i]);

}

}

否则(list2.Count> list.Count)

{

for(int i = 0; i< list.Count; i ++)

{

Response.Write((int)list [i] +(int)list2 [i]);

}

}

//注意:编辑列表项以查看效果
// declare first array list and two items
ArrayList list = new ArrayList();
// you can add your list dynamically here it is manualy added for demo purpuse
list.Add(1);
list.Add(2);
// // declare 2nd array list and 4 items
ArrayList list2 = new ArrayList();
list2.Add(1);
list2.Add(2);
list2.Add(3);
list2.Add(4);
//if codition
if (list.Count > list2.Count)
{
for (int i = 0; i < list2.Count; i++)
{
Response.Write((int)list[i]+(int)list2[i]);
}
}
else if (list2.Count > list.Count)
{
for (int i = 0; i < list.Count; i++)
{
Response.Write((int)list[i] + (int)list2[i]);
}
}
//Note: Edit List items to see the effect


这篇关于如何添加两个动态arraylist的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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