首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请问以个ArryList与流的有关问题

2012-03-05 
请教以个ArryList与流的问题import java.io.IOExceptionimport java.util.ArrayListpublic class test{

请教以个ArryList与流的问题
import java.io.IOException;
import java.util.ArrayList;;
public class test
{
public static void main(String args[])
{
ArrayList al=new ArrayList();
stu st=new stu();
int i=0;
int k=0;
while(k<3)
{
System.out.println("请输入名字");

byte[] b=new byte[100];
try
{
i=System.in.read(b);
}
catch (IOException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}
String flag=new String(b,0,i);
st.setName(flag);


System.out.println("请输入sex");
try
{
i=System.in.read(b);
}
catch (IOException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}
flag=new String(b,0,i);
st.setSex(flag);

System.out.println("请输入age");
try
{
i=System.in.read(b);
}
catch (IOException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}
flag=new String(b,0,i);
st.setAge(flag);
al.add(st);
System.out.println(al.size());
k++;
}

System.out.println("你想查询的人名:");
byte[] b=new byte[100];
try
{
i=System.in.read(b);
}
catch (IOException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}
String flag=new String(b,0,i);
for(int j=0;j<3;j++)
{
st=(stu) al.get(j);
if(flag.equals(st.getName()))
{
System.out.println(flag+"他的年龄是: "+st.getAge()+",性别是: "+st.getSex());
return;
}
else
{
System.out.println("对不起,没有找到你所查询的人!");
}
}
}
}
class stu
{
private String name,sex,age;

public String getAge()
{
return age;
}

public void setAge(String age)
{
this.age = age;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public String getSex()
{
return sex;
}

public void setSex(String sex)
{
this.sex = sex;
}

public String toString()
{
return name+"的年龄是"+age+"性别是"+sex;
}

}

如何将ArryList 存储了五个学生的信息写入当前目录下的student.dat文件中 
  
 

  
 


[解决办法]
想对你说几句话:
一:你的代码层次太乱,没有缩进,看起来就觉得很累
二:你的代码风格不太好,大部分内容都放在MAIN函数里,不是个好习惯,应该分开到不同的函数去.
三:你的命名不太好,大小写方面不太符合规范.

忠言逆耳,呵呵,只是希望你能够写好代码,做个好程序员!!!
-----------------------------------------

Java code
 
/**
* project_name: Test
* package_name: netsource
* package_declaration: package netsource;
* filename: ArratlistTest.java
* author: yuhaiming
* date: 2007-11-15
*/
package netsource;

import  java.io.IOException;
import  java.util.ArrayList;
import java.util.Iterator;
import java.io.*;

public  class  ArratlistTest
{
public static void storedata(ArrayList list){
try{
File fl = new File("student.dat");


FileOutputStream output = new FileOutputStream(fl);
Iterator iter = list.iterator();

while(iter.hasNext()){
stu st = (stu)iter.next();
String outputString = "姓名:"+st.getName()+"性别:"+st.getSex()+"年龄:"+st.getAge();
output.write(outputString.getBytes());
}
System.out.println(fl.getAbsolutePath());
}catch(Exception e){
e.printStackTrace();
}
}
public  static  void  main(String  args[])
{
ArrayList  al=new  ArrayList();
stu  st;
int  i=0;
int  k=0;
while(k <2) {

st=new  stu();
System.out.println( "请输入名字 ");

byte[]  b=new  byte[100];
try
{
i=System.in.read(b);
}
catch  (IOException  e1)
{
//  TODO  自动生成  catch  块
e1.printStackTrace();
}
String  flag=new  String(b,0,i);
st.setName(flag);


System.out.println( "请输入sex ");
try
{
i=System.in.read(b);
}
catch  (IOException  e1)
{
//  TODO  自动生成  catch  块
e1.printStackTrace();
}
  flag=new  String(b,0,i);
  st.setSex(flag);

System.out.println( "请输入age ");
try
{
i=System.in.read(b);
}
catch  (IOException  e1)
{
//  TODO  自动生成  catch  块
e1.printStackTrace();
}
flag=new  String(b,0,i);
st.setAge(flag);
al.add(st);
System.out.println(al.size());
k++;
}

System.out.println( "你想查询的人名: ");
byte[]  b=new  byte[100];
try
{
i=System.in.read(b);
}
catch  (IOException  e1)
{
//  TODO  自动生成  catch  块
e1.printStackTrace();
}
String  flag=new  String(b,0,i);
System.out.println("您想查找的姓名是:"+flag);
boolean found = false;
Iterator iter = al.iterator();
while(iter.hasNext()){
st = (stu)iter.next();
//System.out.println(st);
if(flag.equals(st.getName())){
System.out.println("找到了:"+st.toString());
found =true;
break;
}
}
if(!found)System.out.println("没找到您要查找的数据!");
storedata(al);


/*
for(int  j=0;j <2;j++)
{
st=(stu)  al.get(j);
System.out.println(st.getName());
if(flag.equals(st.getName()))
{
System.out.println(flag+ "他的年龄是:  "+st.getAge()+ ",性别是:  "+st.getSex());
return;
}
else
{
System.out.println( "对不起,没有找到你所查询的人! ");
}
}
*/
}

}

class  stu
{
private  String  name,sex,age;

public  String  getAge()
{
return  age;
}

public  void  setAge(String  age)
{
this.age  =  age;
}

public  String  getName()
{
return  name;
}

public  void  setName(String  name)
{
this.name  =  name;
}

public  String  getSex()


{
return  sex;
}

public  void  setSex(String  sex)
{
this.sex  =  sex;
}

public  String  toString()
{
return  name+ "的年龄是 "+age+ "性别是 "+sex;
}

}


热点排行