原题:
编写一个冒泡排序算法,使其可以用于对数据Vector对象排序。同时,编写程序来测试你的算法
我的 程序如下:
import java.util.Vector;
import java.io.BufferedReader;
import java.io.*;
public class Vector {
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
public static void fillVector(Vector List)throws IOException{
int index=1;
StringTokenizer tokenizer;
tokenizer=new StringTokenizer(keyboard.readLine);
do{
List.addElement(Integer(Integer.parseInt(tokenizer.nextToken)));
if(!tokenizer.hasMoreToken())
index=0;
}while(index==1);
}
public static void sort(Vector list)throws IOException,ArrayIndexOutOfBoundsException {
int num=list.size();
for(int j=1;j<=num-1;j++)
for(int i=0;i<num-j;i++)
if(list.elementAt(i)>list.elementAt(i+1)){
int temp=list.elementAt(i);
list.elementAt(i)=list.elementAt(i+1);
list.elementAt(i+1)=temp;
}
}
public static void print(Vector list){
System.out.println(list);
}
public static void main(String args[])throws IOException{
Vector LIST=new Vector();
fillVector(LIST);
sort(LIST);
print(LIST);
}
}
出错的地方及提示:
import java.util.Vector;////////////The import java.util.Vector conflicts with a type defined in the same file
StringTokenizer tokenizer;//////////StringTokenizer cannot be resolved to a type
tokenizer=new StringTokenizer(keyboard.readLine);////keyboard.readLine cannot be resolved or is not a field
int num=list.size();/////The method size() is undefined for the type Vector
if(list.elementAt(i)>list.elementAt(i+1)){ ////The method elementAt(int) is undefined for the type Vector
后面也是一样
请大侠帮忙啊
编写一个冒泡排序算法,使其可以用于对数据Vector对象排序。同时,编写程序来测试你的算法
我的 程序如下:
import java.util.Vector;
import java.io.BufferedReader;
import java.io.*;
public class Vector {
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
public static void fillVector(Vector List)throws IOException{
int index=1;
StringTokenizer tokenizer;
tokenizer=new StringTokenizer(keyboard.readLine);
do{
List.addElement(Integer(Integer.parseInt(tokenizer.nextToken)));
if(!tokenizer.hasMoreToken())
index=0;
}while(index==1);
}
public static void sort(Vector list)throws IOException,ArrayIndexOutOfBoundsException {
int num=list.size();
for(int j=1;j<=num-1;j++)
for(int i=0;i<num-j;i++)
if(list.elementAt(i)>list.elementAt(i+1)){
int temp=list.elementAt(i);
list.elementAt(i)=list.elementAt(i+1);
list.elementAt(i+1)=temp;
}
}
public static void print(Vector list){
System.out.println(list);
}
public static void main(String args[])throws IOException{
Vector LIST=new Vector();
fillVector(LIST);
sort(LIST);
print(LIST);
}
}
出错的地方及提示:
import java.util.Vector;////////////The import java.util.Vector conflicts with a type defined in the same file
StringTokenizer tokenizer;//////////StringTokenizer cannot be resolved to a type
tokenizer=new StringTokenizer(keyboard.readLine);////keyboard.readLine cannot be resolved or is not a field
int num=list.size();/////The method size() is undefined for the type Vector
if(list.elementAt(i)>list.elementAt(i+1)){ ////The method elementAt(int) is undefined for the type Vector
后面也是一样
请大侠帮忙啊
StringTokenizer在包java.util里,要加上引用
import java.util.Vector;
import java.io.BufferedReader;
import java.io.*;
public class MyVector{
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
public static void fillVector(Vector List)throws IOException{
int index=1;
StringTokenizer tokenizer;
tokenizer=new StringTokenizer(keyboard.readLine);
do{
List.addElement(Integer(Integer.parseInt(tokenizer.nextToken)));
if(!tokenizer.hasMoreToken())
index=0;
}while(index==1);
}
public static void sort(Vector list)throws IOException,ArrayIndexOutOfBoundsException {
int num=list.size();
for(int j=1;j<=num-1;j++)
for(int i=0;i<num-j;i++)
if(list.elementAt(i)>list.elementAt(i+1)){
int temp=list.elementAt(i);
list.elementAt(i)=list.elementAt(i+1);
list.elementAt(i+1)=temp;
}
}
public static void print(Vector list){
System.out.println(list);
}
public static void main(String args[])throws IOException{
Vector LIST=new Vector();
fillVector(LIST);
sort(LIST);
print(LIST);
}
}
还是不行啊
有错啊
tokenizer=new StringTokenizer(keyboard.readLine);//////StringTokenizer cannot be resolved to a type
if(list.elementAt(i)>list.elementAt(i+1)){ /////The operator > is undefined for the argument type(s) java.lang.Object, java.lang.Object
等等.....
怎么办啊
第二个是Object对象,没显式转换类型不能比较大小
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.*;
public class MyVector{
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
public static void fillVector(Vector List)throws IOException{
int index=1;
StringTokenizer tokenizer;
tokenizer=new StringTokenizer(keyboard.readLine);
do{
List.addElement(Integer(Integer.parseInt(tokenizer.nextToken)));
if(!tokenizer.hasMoreToken())
index=0;
}while(index==1);
}
public static void sort(Vector list)throws IOException,ArrayIndexOutOfBoundsException {
int num=list.size();
for(int j=1;j<=num-1;j++)
for(int i=0;i<num-j;i++)
if(list.elementAt(i).equals(list.elementAt(i+1))){
int temp=list.elementAt(i);
list.elementAt(i)=list.elementAt(i+1);
list.elementAt(i+1)=temp;
}
}
public static void print(Vector list){
System.out.println(list);
}
public static void main(String args[])throws IOException{
Vector LIST=new Vector();
fillVector(LIST);
sort(LIST);
print(LIST);
}
}
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.*;
public class MyVector{
static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
public static void fillVector(Vector List)throws IOException{
int index=1;
StringTokenizer tokenizer;
tokenizer=new StringTokenizer(keyboard.readLine);
do{
List.addElement(Integer(Integer.parseInt(tokenizer.nextToken)));
if(!tokenizer.hasMoreToken())
index=0;
}while(index==1);
}
public static void sort(Vector list)throws IOException,ArrayIndexOutOfBoundsException {
int num=list.size();
for(int j=1;j<=num-1;j++)
for(int i=0;i<num-j;i++)
if(((Integer)list.elementAt(i)).intValue() > ((Integer)(list.elementAt(i+1)).intValue())){
int temp=list.elementAt(i);
list.elementAt(i)=list.elementAt(i+1);
list.elementAt(i+1)=temp;
}
}
public static void print(Vector list){
System.out.println(list);
}
public static void main(String args[])throws IOException{
Vector LIST=new Vector();
fillVector(LIST);
sort(LIST);
print(LIST);
}
}