C#中的小麻烦!!!!

Sky夜名 2009-03-22 04:38:01
一个不知是不是问题的问题!!
已知有一个student类,此类有一个实例化对象stu1,stu1中含有name,sex,age和score三个变量。
又知数组a中的a[0]指的是stu1.score。那么如何由a[0]的stu1.score来使用stu1中的其他变量呢??比如stu1.name。。。。
不知这样表达对不对,说的明不明白。。。。
...全文
126 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
nilinliu 2009-07-07
  • 打赏
  • 举报
回复
看看课本就知道了
duanyuncanyang 2009-06-18
  • 打赏
  • 举报
回复
我记得arraylist中有sorted函数的啊,不可以先将其压入arraylist中然后调用sorted函数吗
jiang6519 2009-06-12
  • 打赏
  • 举报
回复
我记得里面有个自带的升降序排序的函数吧,不可以用那个?
crowzwong 2009-03-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wuyi8808 的回复:]
C# code
using System;
using System.Collections;

struct student
{
public string no, name, sex;
public int age, score;

public student(string no, string name, int age, string sex, int score)
{
this.no = no;
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
}

class Program
{
static void Main()
{
student stu1 = new student("2…
[/Quote]

mark
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
using System;
using System.Collections;

struct student
{
public string no, name, sex;
public int age, score;

public student(string no, string name, int age, string sex, int score)
{
this.no = no;
this.name = name;
this.sex = sex;
this.age = age;
this.score = score;
}
}

class Program
{
static void Main()
{
student stu1 = new student("20070001", "王明", 20, "male", 89);
student stu2 = new student("20070002", "王铭", 19, "female", 92);
student stu3 = new student("20070003", "王鸣", 20, "male", 83);
student stu4 = new student("20070004", "王名", 21, "male", 66);
int[] a = new int[] {stu1.score,stu2.score,stu3.score,stu4.score};
student[] st = new student[] {stu1, stu2, stu3, stu4 };
Array.Sort(a, st);
foreach ( student s in st)
{
Console.WriteLine("{0} {1}", s.name, s.score);
}
}
}

/* 输出:
王名 66
王鸣 83
王明 89
王铭 92
*/
itliyi 2009-03-22
  • 打赏
  • 举报
回复
泛型省事但耗性能,用ArrayList 也可以,加入ArrayList集合
强制转换
新建比较类接口Imcompar实现排序
TkingCN 2009-03-22
  • 打赏
  • 举报
回复
指针没用过
我也是菜鸟· 不过我看你的思路 我建议你用泛型集合·
Sky夜名 2009-03-22
  • 打赏
  • 举报
回复
指针行吗
TkingCN 2009-03-22
  • 打赏
  • 举报
回复
不行
数组的含义是 在内存中相同类型的数据连续存放 并且数组有长度限制
要想达到你说的那个目的 建议用泛型集合 把你的实体类当做对象装入集合 再通过集合访问实体类中的属性 这样就OK勒
Sky夜名 2009-03-22
  • 打赏
  • 举报
回复
student stu1 = new student("20070001", "王明", 20, "male", 89);
student stu2 = new student("20070002", "王铭", 19, "female", 92);
student stu3 = new student("20070003", "王鸣", 20, "male", 83);
student stu4 = new student("20070004", "王名", 21, "male", 66);
int[] a = new int[4] {stu1.score,stu2.score,stu3.score,stu4.score};
Array.Sort(a);

以上代码,怎样实现对最后一排score排序后,再按顺序输出所有信息。。。


或者用其他方法实现
tmd456 2009-03-22
  • 打赏
  • 举报
回复
不行
wuyi8808 2009-03-22
  • 打赏
  • 举报
回复
不可以。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧