導航:首頁 > 編程語言 > 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按照首字母排序相關的資料

熱點內容
分布式服務如何跨庫統計數據 瀏覽:829
力控轉發數據客戶端模式如何建立 瀏覽:200
怎麼樣讓自己的網站不被別人看到 瀏覽:711
編程擴展效果如何 瀏覽:335
榮耀暢玩手環同步qq 瀏覽:475
怎麼向sql中添加資料庫 瀏覽:596
錄歌失敗重啟app什麼意思 瀏覽:522
壓縮文件包怎麼在微信發送 瀏覽:432
mysql資料庫怎麼插入時間值 瀏覽:191
微信視頻不能轉發朋友圈 瀏覽:596
影視後期的app有哪些 瀏覽:956
電子保單數據出錯什麼意思 瀏覽:368
如何以文件下載音樂 瀏覽:438
計算機網路章節練習 瀏覽:999
單片機的外部中斷程序 瀏覽:48
表格批量更名找不到指定文件 瀏覽:869
js的elseif 瀏覽:584
3dmaxvray視頻教程 瀏覽:905
imgtool工具中文版 瀏覽:539
java幫助文件在哪裡 瀏覽:965

友情鏈接