java list集合的问题
importjava.util.ArrayList;importjava.util.List;publicclasstest{/***@paramargs*/public...
import java.util.ArrayList;
import java.util.List;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List list = new ArrayList();
// 新建5个student类实例并放入list集合
int[] id = { 1, 2, 3, 4, 5 };
String[] name = { "张山", "李四", "王五", "赵六", "狗屎" };
int[] score1 = { 50, 89, 56, 88, 56 };
int[] score2 = { 89, 79, 90, 98, 67 };
int[] score3 = { 70, 66, 76, 45, 66 };
Student[] stu = new Student[5];
for (int i = 0; i < 5; i++) {
stu[i] = new Student(id[i], name[i], score1[i], score2[i],
score3[i]);
list.add(stu[i]);
}
//删除为名字为赵六的人的资料(不会写了)
System.out.println(list);
}
}
//student类代码get set方法以自动生成
public class Student implements Comparable<Student>
{
private int score1,score2,score3;
private float pscore;
private int id;
private String name;
public Student(int id,String name,int score1,int score2,int score3)
{
this.id=id;
this.name=name;
this.score1=score1;
this.score2=score2;
this.score3=score3;
this.pscore=(this.score1+this.score2+this.score3)/3;
}
//刚刚的提问不知道怎么没了。。。
求能运行的代码 展开
import java.util.List;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List list = new ArrayList();
// 新建5个student类实例并放入list集合
int[] id = { 1, 2, 3, 4, 5 };
String[] name = { "张山", "李四", "王五", "赵六", "狗屎" };
int[] score1 = { 50, 89, 56, 88, 56 };
int[] score2 = { 89, 79, 90, 98, 67 };
int[] score3 = { 70, 66, 76, 45, 66 };
Student[] stu = new Student[5];
for (int i = 0; i < 5; i++) {
stu[i] = new Student(id[i], name[i], score1[i], score2[i],
score3[i]);
list.add(stu[i]);
}
//删除为名字为赵六的人的资料(不会写了)
System.out.println(list);
}
}
//student类代码get set方法以自动生成
public class Student implements Comparable<Student>
{
private int score1,score2,score3;
private float pscore;
private int id;
private String name;
public Student(int id,String name,int score1,int score2,int score3)
{
this.id=id;
this.name=name;
this.score1=score1;
this.score2=score2;
this.score3=score3;
this.pscore=(this.score1+this.score2+this.score3)/3;
}
//刚刚的提问不知道怎么没了。。。
求能运行的代码 展开
展开全部
package com.milkway.tqs.bean;
import java.util.ArrayList;
import java.util.List;
public class Student {
private int id;
private String name;
private int score1;
private int score2;
private int score3;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
public Student(int id, String name, int score1, int score2, int score3) {
super();
this.id = id;
this.name = name;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
}
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
// 初始化5个Student实例并放入list集合
int[] id = { 1, 2, 3, 4, 5 };
String[] name = { "张山", "李四", "王五", "赵六", "狗屎" };
int[] score1 = { 50, 89, 56, 88, 56 };
int[] score2 = { 89, 79, 90, 98, 67 };
int[] score3 = { 70, 66, 76, 45, 66 };
Student[] stu = new Student[5];
for (int i = 0; i < 5; i++) {
stu[i] = new Student(id[i], name[i], score1[i], score2[i],
score3[i]);
list.add(stu[i]);
}
for (int i = 0; i < list.size(); i++) {
// 删除姓名为赵六的人的资料(不会写了)
if ("赵六".equals(list.get(i).getName())) {
list.remove(i);
}
}
System.out.println(list.size());// 打印list集合tostring已写
for (Student student : list) {
System.out.println(student.getName() + ";;;");
}
}
}
运行结果:
4
张山;;;
李四;;;
王五;;;
狗屎;;;
import java.util.ArrayList;
import java.util.List;
public class Student {
private int id;
private String name;
private int score1;
private int score2;
private int score3;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
public Student(int id, String name, int score1, int score2, int score3) {
super();
this.id = id;
this.name = name;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
}
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
// 初始化5个Student实例并放入list集合
int[] id = { 1, 2, 3, 4, 5 };
String[] name = { "张山", "李四", "王五", "赵六", "狗屎" };
int[] score1 = { 50, 89, 56, 88, 56 };
int[] score2 = { 89, 79, 90, 98, 67 };
int[] score3 = { 70, 66, 76, 45, 66 };
Student[] stu = new Student[5];
for (int i = 0; i < 5; i++) {
stu[i] = new Student(id[i], name[i], score1[i], score2[i],
score3[i]);
list.add(stu[i]);
}
for (int i = 0; i < list.size(); i++) {
// 删除姓名为赵六的人的资料(不会写了)
if ("赵六".equals(list.get(i).getName())) {
list.remove(i);
}
}
System.out.println(list.size());// 打印list集合tostring已写
for (Student student : list) {
System.out.println(student.getName() + ";;;");
}
}
}
运行结果:
4
张山;;;
李四;;;
王五;;;
狗屎;;;
追问
list.get(i).点不出getname啊
追答
你泛型下
List list = new ArrayList();
就好了把这段代码考上
你之前代码没泛型
List list = new ArrayList();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询