导航:首页 > 编程语言 > java按照首字母排序

java按照首字母排序

发布时间:2023-10-24 06:32:21

1. java 汉字首字母排序

我知道用java可以通过汉字的获得到首字母。下面是源码:
import net.sourceforge.pinyin4j.PinyinHelper;

public class PinyinAPI {

/**
* 提取每个汉字的首字母(大写)
*
* @param str
* @return
*/
public static String getPinYinHeadChar(String str) {
if (isNull(str)) {
return "";
}
String convert = "";
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
// 提取汉字的首字母
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert += pinyinArray[0].charAt(0);
}
else {
convert += word;
}
}

convert = string2AllTrim(convert);
return convert.toUpperCase();
}

/*
* 判断字符串是否为空
*/

public static boolean isNull(Object strData) {
if (strData == null || String.valueOf(strData).trim().equals("")) {
return true;
}
return false;
}

/**
* 去掉字符串包含的所有空格
*
* @param value
* @return
*/
public static String string2AllTrim(String value) {
if (isNull(value)) {
return "";
}
return value.trim().replace(" ", "");
}

public static void main(String[] args) {
String ss = PinyinAPI.getPinYinHeadChar("中国");
System.out.print(ss);//ZG
}
}

2. JAVA编写程序,对英文单词按照字母顺序进行排序(升序or降序)

Easy!

import java.util.ArrayList;
import java.util.Collections;

public class Main {

/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
String target = "John Bill Peter ";

ArrayList<String> list = new ArrayList<String>();
for (String temp : target.split(" ")) {
list.add(temp);
}

Collections.sort(list);

for(int i = list.size(); i > 0; i--){
System.out.println(list.get(i-1) + " ");
}

}

}

----------------
Peter
John
Bill

3. java 中有城市类 存在arraylist 怎么让它按名字首字母排序

参考代码如下

importjava.util.ArrayList;
importjava.util.Comparator;

publicclassListDemo{
publicstaticvoidmain(String[]args){
ArrayList<String>city=newArrayList<String>();
city.add("Jonesboro");
city.add("Chaohu");
city.add("LittleRock");
city.add("bridgeport");
city.add("Rome");
city.add("chicago");
System.out.println("排序前"+city);
//排序.
city.sort(newComparator<String>(){
//重点是这个函数
publicintcompare(Stringo1,Stringo2){
//忽略掉大小写后,进行字符串的比较
Strings1=o1.toLowerCase();
Strings2=o2.toLowerCase();
returns1.compareTo(s2);//从a-z的排
//returns2.compareTo(s1);//从z-a的排
}
});
System.out.println("排序后"+city);
}
}

运行效果

排序前[Jonesboro,Chaohu,LittleRock,bridgeport,Rome,chicago]
排序后[bridgeport,Chaohu,chicago,Jonesboro,LittleRock,Rome]

4. Java中让用户随机输入一串名字并按照字母顺序输出

importjava.util.Scanner;

publicclassTest{
publicstaticvoidmain(String[]args){
=newScanner(System.in);
System.out.println("输入一组名字:");
Strings=sc.nextLine();
String[]strs=s.split("");
Stringtemp="";
for(inti=0;i<strs.length;i++){
for(intj=i;j>0;j--){
if(strs[j].charAt(0)<strs[j-1].charAt(0)){
temp=strs[j-1];
strs[j-1]=strs[j];
strs[j]=temp;
}else
break;
}
}
for(inti=0;i<strs.length;i++){
System.out.print(strs[i]+"");
}
}
}

5. JAVA通过数组按首字母排序

package demo;

import java.util.Arrays;

public class Test {

public void sortStringArray() {
String[] arrayToSort = new String[] { "Oscar", "Charlie", "Ryan",
"Adam", "David","aff","Aff" };
System.out.println();
System.out.println("字符型数组排序,排序前:");
for (int i = 0; i < arrayToSort.length; i++){
System.out.print(arrayToSort[i]+",");
}
System.out.println();
System.out.println("排序后:");
// 调用数组的静态排序方法sort,且不区分大小写
Arrays.sort(arrayToSort,String.CASE_INSENSITIVE_ORDER);

for (int i = 0; i < arrayToSort.length; i++){
System.out.print(arrayToSort[i]+",");
}
}

public static void main(String[] args) {
new Test().sortStringArray();

}

}
运行结果:
字符型数组排序,排序前:
Oscar,Charlie,Ryan,Adam,David,aff,Aff,
排序后:
Adam,aff,Aff,Charlie,David,Oscar,Ryan,
希望对你有帮助

6. JAVA编程输入三个名字按字母顺序自动排序

	publicstaticvoidmain(String[]args){
List<String>list=newArrayList<String>();
Scannerscan=newScanner(System.in);
System.out.println("Pleaseinputthefirstname:");
list.add(getName(scan.next(),"first"));
System.out.println("Pleaseinputthesecondname:");
list.add(getName(scan.next(),"second"));
System.out.println("Pleaseinputthethirdname:");
list.add(getName(scan.next(),"third"));
scan.close();
//剔除为null的对象(因为不合法的名称被设置为null)。
//此处为何用ite迭代删除?原因参考http://blog.csdn.net/longyulu/article/details/8315068
Iterator<String>ite=list.iterator();
while(ite.hasNext()){
Strings=ite.next();
if(s==null){
ite.remove();
}
}
//排序
Collections.sort(list);
//输出最终结果
System.out.print("Thenamesare:");
for(Strings:list){
System.out.print("""+s+""");
}

}

privatestaticStringgetName(Stringname,Stringindex){
StringregEx="^[A-Za-z]+$";//纯字母正则表达式
Patternpat=Pattern.compile(regEx);
if(pat.matcher(name).matches()){
//全部转为小写
name=name.toLowerCase();
//提取第一个字母并转换大写
Stringfirst=(name.charAt(0)+"").toUpperCase();
name=first+name.substring(1);
System.out.println(name+"isthe"+index+"name.");
}else{
name=null;
System.out.println("Error:The"+index+"namewasnotaccepted.");
}
returnname;
}
//控制台结果:
Pleaseinputthefirstname:
tsKd
Tskdisthefirstname.
Pleaseinputthesecondname:
ukf90
Error:Thesecondnamewasnotaccepted.
Pleaseinputthethirdname:
admin
Administhethirdname.
Thenamesare:"Admin""Tskd"

7. java字符串按照字母顺序输出,悬赏100分,在线等

单例模式

publicclassSingleConvert
{
;
privateStringresult;
privateStringoriginal;

privateSingleConvert()
{}

privatevoidconvertString()
{
charc=result.charAt(0);
if(Character.isUpperCase(c))
{
System.out.println(result);
return;
}
else
{
result=result.substring(1);
convertString();
}
}

privatevoidsortString()
{
char[]cs=result.toCharArray();
for(inti=0;i<cs.length;i++)
{
for(intj=i+1;j<cs.length;j++)
{
charfirst=Character.toUpperCase(cs[i]);
charsecond=Character.toUpperCase(cs[j]);
if(first>second)
{
chartemp=cs[i];
cs[i]=cs[j];
cs[j]=temp;
}
}
}
result=String.valueOf(cs);
System.out.println(result);
}

privatevoidfindMax()
{
if(result.isEmpty())
{
return;
}
Stringtemp=""+original+"";
charmax=0;
intnum=-1;
for(inti=0;i<result.length();i++)
{
charcurrent=result.charAt(i);
intlen=temp.split(current+"").length-1;
if(len>num)
{
max=current;
num=len;
}
}
System.out.println(result+","+max+"="+num);
}

privatevoidrandomString(intn)
{
Stringresult="";
charc='a';
for(inti=0;i<n;i++)
{
intrandom=(int)(Math.random()*25);
doubletype=Math.random();
if(type>0.6)
{
result+=(char)(c+random-32);
}
else
{
result+=(char)(c+random);
}
}
System.out.println(result);
this.original=result;
this.result=result;
}

()
{
if(null==singleConvert)
{
singleConvert=newSingleConvert();
}
returnsingleConvert;
}

publicstaticvoidmain(String[]args)
{
SingleConvertsingleConvert=SingleConvert.getInstance();
singleConvert.randomString(13);
singleConvert.convertString();
singleConvert.sortString();
singleConvert.findMax();
}
}

8. JAVA 通过字符串的split方法,按照空格拆分字符串,之后按首字母排序

public static void main(String args[])
{
String target="John Bill Peter ";

ArrayList<String> list=new ArrayList<String>();
for(String temp:target.split(" "))
{
list.add(temp);
}

Collections.sort(list);

for(String temp:list)
{
System.out.print(temp+" ");
}

}

9. java中怎样把输入的基本英文单词按首字母顺序排列

importjava.util.ArrayList;
importjava.util.Collections;
importjava.util.Comparator;
importjava.util.List;

/**
*规则:大写字母前于小写字母
*
*@authorAdministrator
*
*/
publicclassSortWords{

privatestaticfinalString[]array={"A","B","C","D","E","F","G",
"H","I","J","K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z"};

publicstaticvoidmain(String[]args){
SortWordssw=newSortWords();
List<String>words=newArrayList<String>();
words.add("book");
words.add("ear");
words.add("alert");
words.add("choice");
words.add("name");
words.add("memery");
words.add("fight");
words.add("call");
words.add("joy");
List<String>list=sw.sort(words,"forward");
for(Stringstr:list){
System.out.println(str);
}
}

publicList<String>sort(List<String>words,Stringtype){
List<Word>list=newArrayList<Word>();
List<String>res=newArrayList<String>();
for(Stringword:words){
Wordw=indexNum(word);
list.add(w);
}
switch(type){
case"forward":
Collections.sort(list,newForward());
for(Wordw:list){
res.add(w.getContent());
}
break;
case"back":
Collections.sort(list,newBack());
for(Wordw:list){
res.add(w.getContent());
}
break;
}

returnres;
}

publicWordindexNum(Stringword){
String[]letters=word.split("");
Stringres="";
Wordw=newWord();
Stringletter=word.substring(0,1);
for(inti=0;i<array.length;i++){
Stringpattern=array[i];
if(letter.equalsIgnoreCase(pattern)){
res+=i;
break;
}
}
w.setContent(word);
w.setIndexNum(Integer.parseInt(res));
returnw;
}

{
@Override
publicintcompare(Objectarg0,Objectarg1){
Wordw1=(Word)arg0;
Wordw2=(Word)arg1;
intflag=w1.getIndexNum()-w2.getIndexNum();
returnflag;
}
}

{
@Override
publicintcompare(Objectarg0,Objectarg1){
Wordw1=(Word)arg1;
Wordw2=(Word)arg0;
intflag=w1.getIndexNum()-w2.getIndexNum();
returnflag;
}
}
}

classWord{
privateStringcontent;

privateIntegerindexNum;

publicStringgetContent(){
returncontent;
}

publicvoidsetContent(Stringcontent){
this.content=content;
}

publicIntegergetIndexNum(){
returnindexNum;
}

publicvoidsetIndexNum(IntegerindexNum){
this.indexNum=indexNum;
}

}

阅读全文

与java按照首字母排序相关的资料

热点内容
计算机网络章节练习 浏览:999
单片机的外部中断程序 浏览:48
表格批量更名找不到指定文件 浏览:869
js的elseif 浏览:584
3dmaxvray视频教程 浏览:905
imgtool工具中文版 浏览:539
java帮助文件在哪里 浏览:965
win10切换输入语言 浏览:696
haier电视网络用不了怎么办 浏览:361
苹果6手机id怎么更改 浏览:179
米家扫地机器人下载什么app 浏览:82
如何在编程猫代码岛20种树 浏览:915
手机基础信息存储在哪个文件 浏览:726
如何查找手机备份文件 浏览:792
内存清理工具formac 浏览:323
iphone过滤骚扰电话 浏览:981
wap网络如何使用微信 浏览:699
手机迅雷应用盒子在哪个文件夹 浏览:351
windows8网络连接 浏览:442
怎么快速增加qq群人数 浏览:919

友情链接