我有两个arraylist
public ArrayList myA = new ArrayList();
public ArrayList myB = new ArrayList();
当myA
中有新值时,请删除myB
的旧内容,将旧内容从myA
移至myB
,然后移至myA
新值
例如:
myA("value1","value2","value3");
myB("","","");
来新的价值观:
myA("value4","value5","value6");
myB("value1","value2","value3");
来另一个价值观:
myA("value7","value8","value9");
myB("value4","value5","value6");
请帮帮我
答案 0 :(得分:0)
如果我理解正确,这应该做的工作
// Initial values
ArrayList myA = new ArrayList() { "value1", "value2", "value3" };
ArrayList myB = new ArrayList() { "", "", "" };
// New values
ArrayList myC = new ArrayList() { "value4", "value5", "value6" };
// Clear the old values from myB and add the old values of myA to myB
myB.Clear();
myB.AddRange(myA);
// Clear the old values from mya and add the new values to myA
myA.Clear();
myA.AddRange(myC);
答案 1 :(得分:0)
using System.IO;
using System;
using System.Collections.Generic;
public class ArrayWithBackup : List<String>
{
List<String> arrayWithBackup;
List<String> backupArray;
public ArrayWithBackup()
{
arrayWithBackup = new List<string>();
}
public void AddStrings(List<String> inputs)
{
if (backupArray != null && backupArray.Count > 0)
backupArray.Clear();
backupArray = new List<string>(arrayWithBackup);
arrayWithBackup.Clear();
for (int i = 0; i < inputs.Count; ++i)
{
arrayWithBackup.Add(inputs[i]);
}
}
public void PrintArray()
{
Console.WriteLine("Array contains:");
for(int i = 0; i < arrayWithBackup.Count; ++i)
Console.WriteLine("Index {0}: {1}", i, arrayWithBackup[i]);
}
public void PrintBackupArray()
{
Console.WriteLine("BackupArray contains:");
for (int i = 0; i < backupArray.Count; ++i)
Console.WriteLine("Index {0}: {1}", i, backupArray[i]);
}
public List<String> GetArray()
{
return arrayWithBackup;
}
public List<String> GetBackupArray()
{
return backupArray;
}
}
public class Test
{
static void Main(string[] args)
{
ArrayWithBackup awb = new ArrayWithBackup();
List<String> inputs = new List<String>();
inputs.Add("one");
inputs.Add("two");
inputs.Add("three");
awb.AddStrings(inputs);
inputs.Clear();
inputs.Add("four");
inputs.Add("five");
inputs.Add("six");
awb.AddStrings(inputs);
awb.PrintArray();
awb.PrintBackupArray();
}
}
//Output:
//Array contains:
//Index 0: four
//Index 1: five
//Index 2: six
//BackupArray contains:
//Index 0: one
//Index 1: two
//Index 2: three