//方法一:数组分离
// String[] zmb=new String[9]; //大数组
// String[] zmxb=new String[]{"a","c","u","b","e","p","f","z"};//小数组
// int nindex=0; //小数组的坐标
// for ( int y = 0 ; y < zmxb.length ; y++,nindex++ )
// {
// zmb[y]=zmxb[nindex];
// }
//方法二:增加空值
String[] zmb=new String[]{"a","c","u","b","e","p","f","z",null};
System.out.print("原字符序列:");
for ( int l = 0 ; l < zmb.length ; l++ )
{
System.out.print(zmb[l]+"\t");
}
System.out.println(" ");
int indexty=zmb.length; //保存新数据插入位置,这个值是个初始值,可以为0或者其他
System.out.println("待插入的字符是:");
String numb=input.next(); //输入要插入的字符
//找到新元素的插入位置
System.out.println("zmb.length:"+zmb.length);
for ( int k = 0 ; k < zmb.length ; k++ )
{
System.out.println("hash:"+numb.hashCode()+"\t"+zmb[k].hashCode());
if ( numb.hashCode()<zmb[k].hashCode() )
{
indexty=k;
break;
}
}
System.out.println("indexty:"+indexty);
//元素后移
for ( int j = zmb.length-1 ; j >indexty ; j-- )
{
zmb[j]=zmb[j-1];
}
zmb[indexty]=numb;
System.out.println("插入字符的下标是:"+indexty);
System.out.print("插入后的字符序列是:");
for ( int t = 0 ; t < zmb.length ; t++ )
{
System.out.print(zmb[t]+"\t");
}
//利用Arrays.sort();进行升序排序
Arrays.sort(zmb); //Arrays.sort(zmb);zmb数组中的值不能有空
System.out.print("\n升序排序后: ");
for ( int i = 0 ; i < zmb.length ; i++ )
{
System.out.print(zmb[i]+"\t");
}
System.out.println(" ");
System.out.println("\n");
输入一个字母,并在原有字母的基础上进行排序
最新推荐文章于 2022-03-31 10:22:22 发布