
Map<String, String> map = new HashMap<String, String>();
mapput("ele1", "小樱");
mapput("ele2", "若曦");
mapput("ele3", "晴川");
Set<String> set = mapkeySet();
//Set转List,方法一 : ArrayList(Collection<> c)
List<String> list1 = new ArrayList<String>(set);
for(int i = 0; i < list1size(); i++){
Systemoutprintln("list1(" + i + ") --> " + list1get(i));
}
//Set转List,方法二:List实现类(ArrayList/LinkedList)的方法 -- addAll(Collection<> c)
List<String> list2 = new ArrayList<String> ();
list2addAll(set);
for(String elem : list2){
Systemoutprintln(elem);
}
你在实例化的时候如何做到T是的类型是不确定的?。。。
泛型类是这么用的
public class MyList<T> : List<T>
{
//这里重写一个将T中的属性全显示出来的方法
public override string ToString()
{
string result = stringEmpty;
foreach (T obj in this)
{
result += "[";
PropertyInfo[] ps = objGetType()GetProperties();
//循环获取该对象的所有属性和值
foreach (PropertyInfo info in ps)
{
result += "属性:" + infoName + " ";
result += "值:" + infoGetValue(obj,null)+" ";
}
result += "],";
}
return resultTrimEnd(',');
}
}
然后自定义一个对象Student
public class Student
{
private string _name = stringEmpty;
public string Name
{
set { _name = value; }
get { return _name; }
}
private int _age = 0;
public int Age
{
set { _age = value; }
get { return _age; }
}
}
使用时:
Student s1 = new Student();
s1Name = "stu1";
s1Age = 90;
Student s2 = new Student();
s2Name = "stu2";
s2Age = 70;
MyList<Student> list = new MyList<Student>();
//如果你需要跨页面传递,这里可以选择把list存到Session里
listAdd(s1);
listAdd(s2);
//跨页面的话,要把Session转换回来,这里模拟一下就用上边已经定义的list了
IList<Student> newList=list;
//newListToString()显示所有对象的属性和值,已经在泛型集合里定义好的
ConsoleWriteLine(newListToString());
ConsoleRead();
要搞清楚这个问题,需要先了解c# 的这类写法的语法糖
想使用上述var scene = new Scene() { new Entity() }; 语法,必须满足以下条件
该类实现了SystemCollectionsIEnumerable 接口
该类有void Add(xx value) 一个参数的add 函数
它的本质上是使用了
var scene = new Scene();
sceneAdd(new Entity());
实现的添加,我们可以从反编译的il 代码中看到这一点
所以,在你的代码中
RandomExtract Data { get; set; } = new() { new (1, BaseEquipment()), }
实际上,是先调用了RandomExtract 的空的构造函数构造了一个RandomExtract 实例,然后再一个一个的new 出RandomReward 实例,Add(继承的父类List 提供了两个必要条件) 进Data 中的
所以你在构造函数中,是拦截不到传入的RandomReward 集合的
如果想在构造函数中拦截,你可以使用RandomExtract 的第二个构造函数
RandomExtract(params RandomReward[] item)
修改成
RandomExtract Data { get; set; } = new(
new(1, new BaseEquipment()),
new (2, new BaseEquipment()),
)
package b;
import javautilArrayList;
import javautilHashSet;
import javautilIterator;
import javautilList;
import javautilSet;
public class LianXi2 {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList(); //使用ArrayList保存所有学生的信息,要求指明泛型类为Student,集合对象为list
listadd(new Student("Tom", 18, 100, "class05"));
listadd(new Student("Jerry", 22, 70, "class04"));
listadd(new Student("Owen", 25, 90, "class05"));
listadd(new Student("Jim", 30,80 , "class05"));
listadd(new Student("Steve", 28, 66, "class06"));
listadd(new Student("Kevin", 24, 100, "class04"));
int sage=0;//统计age的和
Iterator it = listIterator(); //创建学生列表的迭代器对象it,并指明泛型类型为Student
Set<String> bjSet= new HashSet();//使用HashSet集合对象,保存班级编号,指明泛型为String。
while( ithasNext()){ //使用迭代器,遍历学生列表。
Student s= itnext();//获取每个学生对象
sage+=sgetAge(); //累加每个学生的成绩
bjSetadd(sgetClassNum());//把学生的班级保存到bjSet集合中
}
Systemoutprintln("平均年龄:" + sage/listsize());//以格式“平均年龄:26”,输出平均成绩
//使用二重循环遍历每个班级的学生,统计出每个班级的学生的成绩,和学生的个数
for(String bj:bjSet){ //使用增强型for遍历每个班级
int score=0;//保存每个班级的成绩
int count=0;//保存每个班级的学生数量
for(Student s:list){ //使用增强型for遍历每个学生
if( sgetClassNum()equal(bj)){//判断学生是否在这个班级中
score+=sgetScore(); //累加成绩
count+=1; //学生计数
}
}
Systemoutprintln(bj + "---平均成绩---" + score/count);//以格式“class06---平均成绩---66”格式输出成绩
}
}
}
class Student{
private String name;
private int age;
private int score;
private String classNum;
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
thisage = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
thisscore = score;
}
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
thisclassNum = classNum;
}
public Student(String name, int age, int score, String classNum) {
super();
thisname = name;
thisage = age;
thisscore = score;
thisclassNum = classNum;
}
public Student() {
super();
}
}
用递归
直到找到userid=1的时候再退出
package src;
import javautilArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<User> li = new ArrayList<User>();
User u1 = new User();
User u2 = new User();
User u3 = new User();
User u4 = new User();
User u5 = new User();
User result = new User();
u1setUserId(1);
u1setName("u1");
u2setUserId(2);
u2setName("u2");
u3setUserId(3);
u3setName("u3");
u4setUserId(4);
u4setName("u4");
u5setUserId(5);
u5setName("u6");
liadd(u1);
liadd(u2);
liadd(u3);
liadd(u4);
liadd(u5);
result = findById(li, 4);
Systemoutprintln(resultgetUserId() + " " + resultgetName());
}
public static User findById(ArrayList<User> li, int id) {
User user = liremove(0);
if (usergetUserId() == id)
return user;
else
return findById(li, id);
}
}
class User {
private int userId;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
thisuserId = userId;
}
}
测试过了好用的
Arraylist中add()是添加元素的作用,set()是更改元素的作用。
e set(int index,e element):用指定的元素替换列表中指定位置的元素。返回值是通用的(由e决定)。
有两种加法,两种方法的参数和返回值不同。
boolean add(e e):将指定的元素(e)添加到此列表的末尾,并返回布尔类型。
void add(int index,e element):将指定的元素(element)插入此列表中的指定位置(index)。将当前处于该位置的元素(如果有)和所有后续元素移动到右侧(将其索引添加到1),没有返回值。
扩展资料:
arraylist的常用方法是:
void clear():从列表中删除所有元素。
boolean contains(object o):如果此列表包含指定元素,则返回true。
e get(int index):返回此列表中指定位置的元素。
布尔删除(对象O):删除此列表中指定元素的第一个匹配项(如果存在)。
int size():返回此列表中的元素数。
boolean isempty():如果此列表中没有元素,则为true
int index of(object o):返回第一次出现在此列表中的指定元素的索引,如果此列表不包含元素,则返回-1。
int last index of(object o):返回此列表中指定元素最后一次出现的索引,如果此列表不包含索引,则返回-1。
参考资料来源:百度百科-arraylist
java集合的主要分为三种类型:
Set(集)
List(列表)
Map(映射)
要深入理解集合首先要了解下我们熟悉的数组:
数组是大小固定的,并且同一个数组只能存放类型一样的数据(基本类型/引用类型),而JAVA集合可以存储和 *** 作数目不固定的一组数据。 所有的JAVA集合都位于 javautil包中! JAVA集合只能存放引用类型的的数据,不能存放基本数据类型。
简单说下集合和数组的区别:(参考文章:《Thinking In Algorithm》03数据结构之数组)
Java所有“存储及随机访问一连串对象”的做法,array是最有效率的一种。
1、
效率高,但容量固定且无法动态改变。
array还有一个缺点是,无法判断其中实际存有多少元素,length只是告诉我们array的容量。
2、Java中有一个Arrays类,专门用来 *** 作array。
arrays中拥有一组static函数,
equals():比较两个array是否相等。array拥有相同元素个数,且所有对应元素两两相等。
fill():将值填入array中。
sort():用来对array进行排序。
binarySearch():在排好序的array中寻找元素。
Systemarraycopy():array的复制。
若撰写程序时不知道究竟需要多少对象,需要在空间不足时自动扩增容量,则需要使用容器类库,array不适用。所以就要用到集合。
那我们开始讨论java中的集合。
集合分类:
Collection:List、Set
Map:HashMap、HashTable
以上就是关于java中set怎么转换成list全部的内容,包括:java中set怎么转换成list、asp.net 如何取出List<T>中每一项的值、C# 继承List<T>怎么获取初始赋值内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)