用C#代码,自定义泛型集合,泛型集合类中需要实现两个元素的交换,谁能帮我实现下代码,作业,急!!!
答案:3 悬赏:40 手机版
解决时间 2021-06-06 04:17
- 提问者网友:却不属于对方
- 2021-06-05 11:32
用C#代码,自定义泛型集合,泛型集合类中需要实现两个元素的交换,谁能帮我实现下代码,作业,急!!!
最佳答案
- 五星知识达人网友:你可爱的野爹
- 2021-06-05 12:19
using System.Collections;
using System.Collections.Generic;
using System;
class MyCollection<T> : IEnumerable<T> {
int idx;
int len;
T[] a;
public MyCollection()
{
a = null;
idx = len = 0;
}
public MyCollection(int n)
{
a = new T[n];
idx = 0;
len = n;
}
public void Add( T t )
{
if (idx == len) {
T[] tmp = new T[len + 20];
for (int i = 0; i < len; ++i)
tmp[i] = a[i];
a = tmp;
len += 20;
}
a[idx++] = t;
}
public int Length
{
get { return idx; }
}
public void Swap( int indexA, int indexB )
{
T t = a[indexA];
a[indexA] = a[indexB];
a[indexB] = t;
}
public IEnumerator<T> GetEnumerator()
{
for( int i = 0; i < idx; ++i )
yield return a[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class test {
static void Main()
{
MyCollection<int> a = new MyCollection<int>();
for (int i = 0; i < 10; ++i)
a.Add(i);
foreach (int i in a)
Console.Write("{0} ", i);
for (int i = 0; i < a.Length/2; ++i) {
a.Swap(i, a.Length - i - 1);
}
Console.WriteLine();
foreach (int i in a)
Console.Write("{0} ", i);
}
}
using System.Collections.Generic;
using System;
class MyCollection<T> : IEnumerable<T> {
int idx;
int len;
T[] a;
public MyCollection()
{
a = null;
idx = len = 0;
}
public MyCollection(int n)
{
a = new T[n];
idx = 0;
len = n;
}
public void Add( T t )
{
if (idx == len) {
T[] tmp = new T[len + 20];
for (int i = 0; i < len; ++i)
tmp[i] = a[i];
a = tmp;
len += 20;
}
a[idx++] = t;
}
public int Length
{
get { return idx; }
}
public void Swap( int indexA, int indexB )
{
T t = a[indexA];
a[indexA] = a[indexB];
a[indexB] = t;
}
public IEnumerator<T> GetEnumerator()
{
for( int i = 0; i < idx; ++i )
yield return a[i];
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class test {
static void Main()
{
MyCollection<int> a = new MyCollection<int>();
for (int i = 0; i < 10; ++i)
a.Add(i);
foreach (int i in a)
Console.Write("{0} ", i);
for (int i = 0; i < a.Length/2; ++i) {
a.Swap(i, a.Length - i - 1);
}
Console.WriteLine();
foreach (int i in a)
Console.Write("{0} ", i);
}
}
全部回答
- 1楼网友:十年萤火照君眠
- 2021-06-05 14:58
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ceshi { class Class1 { string age;
public string Age { get { return age; } set { address = value; } } string address;
public string Address { get { return address; } set { age = value; } } } }
List<Class1> list = new List<Class1>(); Class1 f = new Class1(); f.Address(); f.Age(); list.Add(f);
- 2楼网友:渊鱼
- 2021-06-05 13:49
好 我给你
假设是个Class类的集合 好吧
List<Class> list = new List<Class>();
Class c1 = new Class("1年级1班");
Class c2 = new Class("5年级4班");
list.Add(c1);
list.Add(c2);
//交换元素
Class temp = list[0];//定义临时变量
list.RemoveAt(0);//删除第一个,后一个元素自动往前补
list.Add(temp);//交换完成
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯