已知动态数组(ArrayList)有一个student对象但是学生对象里面的score属性没有值,请将student对象的score属性赋值,然后从动态数组里面取出学生对象,打印学生对象 我实在是搞不弄这个题的意义,按我的写发就是以下代码,似乎太简单了import java.util.*;public class Student {
String name = "张三";
String sex = "男";
int id = 1;
int score;
}
public class Arr { public static void main(String[] args) {
Student student = new Student();
ArrayList arr = new ArrayList();
arr.add(student);//ArrayList中存有一个没有score的Student的类对象
Student s = (Student) arr.get(0);//得到这个对象
s.score = 90; //给score赋值
System.out.println("学号:"+s.id);
System.out.println("姓名:"+s.name);
System.out.println("性别:"+s.sex);
System.out.println("成绩:"+s.score); }}
String name = "张三";
String sex = "男";
int id = 1;
int score;
}
public class Arr { public static void main(String[] args) {
Student student = new Student();
ArrayList arr = new ArrayList();
arr.add(student);//ArrayList中存有一个没有score的Student的类对象
Student s = (Student) arr.get(0);//得到这个对象
s.score = 90; //给score赋值
System.out.println("学号:"+s.id);
System.out.println("姓名:"+s.name);
System.out.println("性别:"+s.sex);
System.out.println("成绩:"+s.score); }}
解决方案 »
- 怎么把数据源从Spring配置文件移动到properties文件就不行了?
- 一道简单java 编程题,谁答案让我满意,我就把分全给谁!
- 优化下这个正则
- 新手请问如何JAVA的MyInput的readChar和readString怎么写
- eclipse开发的swing程序,在命令行运行时无故报错
- update date类型的数据之后抛出如下异常,请问我犯了什么错??
- 我用JOptionPane.showMessageDialog(frame,"Be careful!",JOptionPane.ERROR_MESSAGE);来弹出一个消息框,但是里面默认的字体和字号很不
- 关于“张孝祥”JAVA教程的质量问题!!知道的进
- 为什么两次的indexOf结果一样?
- JDBC编程
- 请问JVM是与操作系统绑定的还是与应用程序绑定的?
- 有没有会osworkflow的人啊?帮忙解释下什么意思.
package test;import java.util.ArrayList;public class StudentTest
{
public static void main(String[] args)
{
ArrayList<Object> arr=new ArrayList<Object>();
Student stu=new Student();
stu.setName("studentA");
stu.setId(1);
arr.add("wuxiao0313");
arr.add(stu);
arr.add(new Integer(222));
for(Object o : arr)
{
if(o.getClass().getName().equals("test.Student"))
{
((Student)o).setScore(100);
System.out.println(o.toString());
}
}
}
}
class Student
{
String name,sex;
int id,score;
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 getScore()
{
return score;
}
public void setScore(int score)
{
this.score = score;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
@Override
public String toString()
{
return "名字为:"+this.getName()+"成绩为:"+this.getScore();
}
}
if(o.getClass().getName().equals("test.Student")) 可以改为
if(o instanceof Student) 比较好!
....
不过,一般一个list内应该存放同类型的对象,毕竟容器只是容器,只担任数据存取结构的功能;如果什么都放,除非是必须的,不然这样设计上就有点问题了
应该是一个文件里只能有一个public类