问一道算法题 给你个思路,先用Arrays.sort排序,再分组 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果数组里存的不是数值,是字符串怎么办呢?我写了一个程序,似乎有点问题,可以帮我看看么?/* *将一个数组分成几个Vector,相同的数值放在同一个Vector中 */import java.util.Vector;public class Sort{ public static void main(String[] args){ String[] array={"a","b","a","b","c","c","b","c","a","a","c","d","d"}; Vector sortArray=new Vector(); //存放分组 Vector v0=new Vector(); v0.add(array[0]); sortArray.add(v0); Vector strV=new Vector(); //存放已存在的各不相同的值,找到不同的就放进去 strV.add(array[0]); boolean hasEquals=false; //是否找到相同的 int n=0; for(int i=1;i<array.length;i++){ for(n=0;n<strV.size();n++){ if( array[i].equals((String)strV.elementAt(n)) ){ ( (Vector)(sortArray.elementAt(n)) ).add(array[i]); hasEquals=true; //break; } } if(!hasEquals){ //Vector vnadd1=new Vector(); //vnadd1.add(array[i]); //sortArray.add(vnadd1); sortArray.add(new Vector()); ( (Vector)sortArray.elementAt(n) ).add(array[i]); strV.add(array[i]); } } //下面打印测试 System.out.println("strV.size:"+strV.size()); System.out.println("sortArray.size:"+sortArray.size()); for(int j=0;j<strV.size();j++){ System.out.println( (String)strV.elementAt(j) ); } for(int i=0;i<sortArray.size();i++){ for(int j=0;j<((Vector)sortArray.elementAt(i)).size();j++){ System.out.println(i+" "+j+" "+(String)((Vector)sortArray.elementAt(i)).elementAt(j) ); } } }} Your code is definitely inefficient.This is an extremely simple problem. I give your some tips:1. Use Hashtable instead of Vector;2. Understanding equals() method in Object.If you think careful above tips, you should know how to do that. 想得到一个txt中某行的行号 求助,帮忙看一下一个C语言函数翻译成java语言 广告系统如何实现 日期问题~请帮忙 请教MYSQL问题 setSelectedIcon 这个命令是干什么用的??十分不明白 文件服务器?/目录服务器? tomcat 6.0.10中开启(/servlet/*)功能 有没有关于JBUIDER5的参考书?要好的 cmd编译显示找不到符号,求指导 为什么下载的邮件附件打开时是乱码,直接打开也是一样的? 怎样在".txt"文本中以行为单位插入、读取数据?
*将一个数组分成几个Vector,相同的数值放在同一个Vector中
*/import java.util.Vector;public class Sort{
public static void main(String[] args){
String[] array={"a","b","a","b","c","c","b","c","a","a","c","d","d"};
Vector sortArray=new Vector(); //存放分组
Vector v0=new Vector();
v0.add(array[0]);
sortArray.add(v0);
Vector strV=new Vector(); //存放已存在的各不相同的值,找到不同的就放进去
strV.add(array[0]);
boolean hasEquals=false; //是否找到相同的
int n=0;
for(int i=1;i<array.length;i++){
for(n=0;n<strV.size();n++){
if( array[i].equals((String)strV.elementAt(n)) ){
( (Vector)(sortArray.elementAt(n)) ).add(array[i]);
hasEquals=true;
//break;
}
}
if(!hasEquals){
//Vector vnadd1=new Vector();
//vnadd1.add(array[i]);
//sortArray.add(vnadd1);
sortArray.add(new Vector());
( (Vector)sortArray.elementAt(n) ).add(array[i]);
strV.add(array[i]);
}
}
//下面打印测试
System.out.println("strV.size:"+strV.size());
System.out.println("sortArray.size:"+sortArray.size());
for(int j=0;j<strV.size();j++){
System.out.println( (String)strV.elementAt(j) );
}
for(int i=0;i<sortArray.size();i++){
for(int j=0;j<((Vector)sortArray.elementAt(i)).size();j++){
System.out.println(i+" "+j+" "+(String)((Vector)sortArray.elementAt(i)).elementAt(j) );
}
}
}
}
1. Use Hashtable instead of Vector;
2. Understanding equals() method in Object.If you think careful above tips, you should know how to do that.