JAVA vector 的查询与删除
importjava.io.*;importjava.util.*;publicclassStudent{publicstaticfinalVectorvector=ne...
import java.io.*;
import java.util.*;
public class Student
{
public static final Vector vector = new Vector(10,5);
public Student()
{
}
public static void main(String[] args)
{
menu();
int tag=10;
new Student();
while(true)
{
String s=readInput();
tag=Integer.parseInt(s);
switch(tag)
{
case 1:
{
query();
};break;
case 2:
{
add();
};break;
case 3:
{
delete();
};break;
case 4:
{
viewAll();
};break;
case 5:
{
System.exit(0);
};break;
}
}
}
public static String readInput()
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
return in.readLine();
}
catch(IOException e){}
return "";
}
public static void query()
{
System.out.println("please input the student's ID.:");
}
public static void add()
{
}
public static void delete()
{
System.out.println("\t input the ID you want to delete:");
String i=readInput();
}
public static void viewAll()
{
vector.add(new Student1("001","a.mo",90,89));
vector.add(new Student1("002","b.mo",90,47));
vector.add(new Student1("003","c.mo",99,56));
for(int i=0;i<vector.size();i++)
{
Student1 student = (Student1)vector.get(i);
System.out.println("\tID:"+student.getId());
System.out.println("\tname:"+student.getName());
System.out.println("\tMath:"+student.getScore());
System.out.println("\tEnglish:"+student.getScore1()+"\n");
}
menu();
}
public static void menu()
{
System.out.println("\t welcome to my student's score system!");
System.out.println("\t*********************************************");
System.out.println("\t************* 1.Query ****************");
System.out.println("\t************* 2.Add ****************");
System.out.println("\t************* 3.Delete ****************");
System.out.println("\t************* 4.View All ****************");
System.out.println("\t************* 5.Exit ****************");
System.out.println("\t*********************************************");
}
}
class Student1
{
private String id;
private String name;
private float score;
private float score1;
public Student1(String id,String name,float score,float score1)
{
this.name = name;
this.id = id;
this.score = score;
this.score1 = score1;
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
public float getScore()
{
return score;
}
public float getScore1()
{
return score1;
}
public void setId(String id)
{
this.id = id;
}
public void setName(String name)
{
this.name = name;
}
public void setScore(float score)
{
this.score = score;
}
public void setScore1(float score1)
{
this.score = score1;
}
}该程序,如何实现?
System.out.println("please input the student's math's score.:");
String s=readInput();
float s2=Float.parseFloat(s);
for(int i=0;i<vector.size();i++)
{
Student1 student = (Student1)vector.get(i);
if(s2==student.getScore())
{
vector.removeElementAt(i) ;
System.out.println("Delete success!");
}
else
System.out.println("cannot find !");
请告诉我为什么会这样子,谢谢了。现在查找与删除就这个STRING类型不能匹配了。 展开
import java.util.*;
public class Student
{
public static final Vector vector = new Vector(10,5);
public Student()
{
}
public static void main(String[] args)
{
menu();
int tag=10;
new Student();
while(true)
{
String s=readInput();
tag=Integer.parseInt(s);
switch(tag)
{
case 1:
{
query();
};break;
case 2:
{
add();
};break;
case 3:
{
delete();
};break;
case 4:
{
viewAll();
};break;
case 5:
{
System.exit(0);
};break;
}
}
}
public static String readInput()
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
return in.readLine();
}
catch(IOException e){}
return "";
}
public static void query()
{
System.out.println("please input the student's ID.:");
}
public static void add()
{
}
public static void delete()
{
System.out.println("\t input the ID you want to delete:");
String i=readInput();
}
public static void viewAll()
{
vector.add(new Student1("001","a.mo",90,89));
vector.add(new Student1("002","b.mo",90,47));
vector.add(new Student1("003","c.mo",99,56));
for(int i=0;i<vector.size();i++)
{
Student1 student = (Student1)vector.get(i);
System.out.println("\tID:"+student.getId());
System.out.println("\tname:"+student.getName());
System.out.println("\tMath:"+student.getScore());
System.out.println("\tEnglish:"+student.getScore1()+"\n");
}
menu();
}
public static void menu()
{
System.out.println("\t welcome to my student's score system!");
System.out.println("\t*********************************************");
System.out.println("\t************* 1.Query ****************");
System.out.println("\t************* 2.Add ****************");
System.out.println("\t************* 3.Delete ****************");
System.out.println("\t************* 4.View All ****************");
System.out.println("\t************* 5.Exit ****************");
System.out.println("\t*********************************************");
}
}
class Student1
{
private String id;
private String name;
private float score;
private float score1;
public Student1(String id,String name,float score,float score1)
{
this.name = name;
this.id = id;
this.score = score;
this.score1 = score1;
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
public float getScore()
{
return score;
}
public float getScore1()
{
return score1;
}
public void setId(String id)
{
this.id = id;
}
public void setName(String name)
{
this.name = name;
}
public void setScore(float score)
{
this.score = score;
}
public void setScore1(float score1)
{
this.score = score1;
}
}该程序,如何实现?
System.out.println("please input the student's math's score.:");
String s=readInput();
float s2=Float.parseFloat(s);
for(int i=0;i<vector.size();i++)
{
Student1 student = (Student1)vector.get(i);
if(s2==student.getScore())
{
vector.removeElementAt(i) ;
System.out.println("Delete success!");
}
else
System.out.println("cannot find !");
请告诉我为什么会这样子,谢谢了。现在查找与删除就这个STRING类型不能匹配了。 展开
5个回答
展开全部
1、你这程序对vector不会有任何改动,因为vector的声明里面说的非常明白了:fianl,禁止任何形式的改动,所以,你查看可以、取值可以,但是删除添加等操作绝对不可能完成。
2、Vector中的每个单独数据都是一个Element,你可以在里面存任何类型的数据:字符串、int数值、byte数值、图片、graphics画笔、数组、Vector以及各种自定义类等等都可以。removeElement()就是移除这些你存进来的数据对象,参数是需要移除的数据,Vector会查找符合形参的元素,然后将它删除,如果找不到不进行任何操作;如果想输入一个数字删除一个元素,那么应该使用removeElementAt(int index)这个方法,将指定索引位置index处的数据删除,不管他是什么。
3、修改后的程序如下:
import java.io.*;
import java.util.*;
import javax.swing.JList ;
mport java.io.*;
import java.util.*;
public class Student
{
public static Vector vector = new Vector(10,5);
public Student()
{}
public static void main(String[] args)
{
menu();
int tag=10;
new Student();
while(true)
{
String s=readInput();
tag=Integer.parseInt(s);
switch(tag)
{
case 1:
{
query();
};break;
case 2:
{
add();
};break;
case 3:
{
delete();
};break;
case 4:
{
viewAll();
};break;
case 5:
{
System.exit(0);
};break;
}
}
}
public static String readInput()
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
return in.readLine();
}
catch(IOException e){}
return "";
}
public static void query()
{
JList list = new JList(vector) ;//list就是当前学生的列表了.
System.out.println("please input the student's ID.:");
}
public static void add()
{
Student1 student = new Student1("张三",0,80,80.5) ;
vector.addElement(student) ;
vector.add(new Student1("001","a.mo",90,89));
vector.add(new Student1("002","b.mo",90,47));
vector.add(new Student1("003","c.mo",99,56));
}
public static void delete()
{
System.out.println("\t input the ID you want to delete:");
String i=readInput();
int index = vector.indexOf(i) ;
if(index < 0 || index >= vector.size())
System.out.println("没有此数据!");
vector.removeElementAt(index) ;
}
public static void viewAll()
{
for(int i=0;i<vector.size();i++)
{
Student1 student = (Student1)vector.elementAt(i);
System.out.println("\tID:"+student.getId());
System.out.println("\tname:"+student.getName());
System.out.println("\tMath:"+student.getScore());
System.out.println("\tEnglish:"+student.getScore1()+"\n");
}
menu();
}
public static void menu()
{
System.out.println("\t welcome to my student's score system!");
System.out.println("\t*********************************************");
System.out.println("\t************* 1.Query ****************");
System.out.println("\t************* 2.Add ****************");
System.out.println("\t************* 3.Delete ****************");
System.out.println("\t************* 4.View All ****************");
System.out.println("\t************* 5.Exit ****************");
System.out.println("\t*********************************************");
}
}
class Student1
{
private String id;
private String name;
private float score;
private float score1;
public Student1(String id,String name,float score,float score1)
{
this.name = name;
this.id = id;
this.score = score;
this.score1 = score1;
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
public float getScore()
{
return score;
}
public float getScore1()
{
return score1;
}
public void setId(String id)
{
this.id = id;
}
public void setName(String name)
{
this.name = name;
}
public void setScore(float score)
{
this.score = score;
}
public void setScore1(float score1)
{
this.score = score1;
}
public String toString()
{
return "name = " + name + " , id = " + id + " , score = " + score + " , score1 = " + score1 ;
}
}
2、Vector中的每个单独数据都是一个Element,你可以在里面存任何类型的数据:字符串、int数值、byte数值、图片、graphics画笔、数组、Vector以及各种自定义类等等都可以。removeElement()就是移除这些你存进来的数据对象,参数是需要移除的数据,Vector会查找符合形参的元素,然后将它删除,如果找不到不进行任何操作;如果想输入一个数字删除一个元素,那么应该使用removeElementAt(int index)这个方法,将指定索引位置index处的数据删除,不管他是什么。
3、修改后的程序如下:
import java.io.*;
import java.util.*;
import javax.swing.JList ;
mport java.io.*;
import java.util.*;
public class Student
{
public static Vector vector = new Vector(10,5);
public Student()
{}
public static void main(String[] args)
{
menu();
int tag=10;
new Student();
while(true)
{
String s=readInput();
tag=Integer.parseInt(s);
switch(tag)
{
case 1:
{
query();
};break;
case 2:
{
add();
};break;
case 3:
{
delete();
};break;
case 4:
{
viewAll();
};break;
case 5:
{
System.exit(0);
};break;
}
}
}
public static String readInput()
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
return in.readLine();
}
catch(IOException e){}
return "";
}
public static void query()
{
JList list = new JList(vector) ;//list就是当前学生的列表了.
System.out.println("please input the student's ID.:");
}
public static void add()
{
Student1 student = new Student1("张三",0,80,80.5) ;
vector.addElement(student) ;
vector.add(new Student1("001","a.mo",90,89));
vector.add(new Student1("002","b.mo",90,47));
vector.add(new Student1("003","c.mo",99,56));
}
public static void delete()
{
System.out.println("\t input the ID you want to delete:");
String i=readInput();
int index = vector.indexOf(i) ;
if(index < 0 || index >= vector.size())
System.out.println("没有此数据!");
vector.removeElementAt(index) ;
}
public static void viewAll()
{
for(int i=0;i<vector.size();i++)
{
Student1 student = (Student1)vector.elementAt(i);
System.out.println("\tID:"+student.getId());
System.out.println("\tname:"+student.getName());
System.out.println("\tMath:"+student.getScore());
System.out.println("\tEnglish:"+student.getScore1()+"\n");
}
menu();
}
public static void menu()
{
System.out.println("\t welcome to my student's score system!");
System.out.println("\t*********************************************");
System.out.println("\t************* 1.Query ****************");
System.out.println("\t************* 2.Add ****************");
System.out.println("\t************* 3.Delete ****************");
System.out.println("\t************* 4.View All ****************");
System.out.println("\t************* 5.Exit ****************");
System.out.println("\t*********************************************");
}
}
class Student1
{
private String id;
private String name;
private float score;
private float score1;
public Student1(String id,String name,float score,float score1)
{
this.name = name;
this.id = id;
this.score = score;
this.score1 = score1;
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
public float getScore()
{
return score;
}
public float getScore1()
{
return score1;
}
public void setId(String id)
{
this.id = id;
}
public void setName(String name)
{
this.name = name;
}
public void setScore(float score)
{
this.score = score;
}
public void setScore1(float score1)
{
this.score = score1;
}
public String toString()
{
return "name = " + name + " , id = " + id + " , score = " + score + " , score1 = " + score1 ;
}
}
2009-09-24
展开全部
public static final Vector<student1> vector = new Vector()<student>;这样声明嘛,然后vector。addelement(new student1(???));添加信息呗,查询就是for循环检查vector。elementAt(i)。id==(ID);vector.removeElement()这个就是先用id查询到一个element然后vector.removeElement(vector。elementAt(i));就这样删除啦。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-09-24
展开全部
该程序,如何实现?
CPU执行2进制码实现!这个回答满意否?
-----------这样也才是完整的问题散
你去看下vector类,他是继承List的。他的removeElement()方法是按照你写入顺序,建议使用HashMap
CPU执行2进制码实现!这个回答满意否?
-----------这样也才是完整的问题散
你去看下vector类,他是继承List的。他的removeElement()方法是按照你写入顺序,建议使用HashMap
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
该程序,如何实现?
CPU执行2进制码实现!这个回答满意否?
-----------这样也才是完整的问题散
CPU执行2进制码实现!这个回答满意否?
-----------这样也才是完整的问题散
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序可以编译运行,只是逻辑上欠考虑
做的不错
做的不错
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询