Java實(shí)現(xiàn)字符串反轉(zhuǎn)
第一種:
public class Main {
public static void main(String[] args) {
String s1 = "asdfghjkl";
System.out.println(new StringBuilder(s1).reverse().toString());
}
}第二種:
public class Main {
public static void main(String[] args) {
String s1 = "asdfghjkl";
String[] s = s1.split("");
List<String> list = list = Arrays.asList(s);
Collections.reverse(list);
System.out.println(list);
}
}第三種:
public class Main {
public static void main(String[] args) {
String s1 = "asdfghjkl"; System.out.println(new Main().swapWords(s1));
}
public void swap(char[] arr, int begin, int end) {
while (begin < end) {
char temp = arr[begin];
arr[begin] = arr[end];
arr[end] = temp;
begin++;
end--;
}
}
public String swapWords(String str) {
char[] arr = str.toCharArray();
swap(arr, 0, arr.length - 1);
int begin = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == ' ') {
swap(arr, begin, i - 1);
begin = i + 1;
}
}
return new String(arr);
}
}到此這篇關(guān)于Java實(shí)現(xiàn)字符串反轉(zhuǎn)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
完美解決IDEA Ctrl+Shift+f快捷鍵突然無(wú)效的問(wèn)題
這篇文章主要介紹了完美解決IDEA Ctrl+Shift+f快捷鍵突然無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Java8中的LocalDateTime你會(huì)使用了嗎
LocalDateTime?是?Java?8?中日期時(shí)間?API?提供的一個(gè)類,在日期和時(shí)間的表示上提供了更加豐富和靈活的支持,本文就來(lái)講講LocalDateTime的一些具體使用方法吧2023-05-05
一文快速了解spring?boot中的@idempotent注解
idempotence注解是RESTful API設(shè)計(jì)中一個(gè)重要的概念,它可以保證操作的可靠性和一致性,下面這篇文章主要給大家介紹了關(guān)于spring?boot中@idempotent注解的相關(guān)資料,需要的朋友可以參考下2024-01-01
詳解Spring?Security?捕獲?filter?層面異常返回我們自定義的內(nèi)容
Spring?的異常會(huì)轉(zhuǎn)發(fā)到?BasicErrorController?中進(jìn)行異常寫入,然后才會(huì)返回客戶端。所以,我們可以在?BasicErrorController?對(duì)?filter異常進(jìn)行捕獲并處理,下面通過(guò)本文給大家介紹Spring?Security?捕獲?filter?層面異常,返回我們自定義的內(nèi)容,感興趣的朋友一起看看吧2022-05-05
Java實(shí)現(xiàn)帶頭結(jié)點(diǎn)的單鏈表
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)帶頭結(jié)點(diǎn)的單鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
mybatis模糊查詢之bind標(biāo)簽和concat函數(shù)用法詳解
大家都知道bind 標(biāo)簽可以使用 OGNL 表達(dá)式創(chuàng)建一個(gè)變量井將其綁定到上下文中,接下來(lái)通過(guò)本文給大家介紹了mybatis模糊查詢——bind標(biāo)簽和concat函數(shù)用法,需要的朋友可以參考下2022-08-08
關(guān)于Elasticsearch封裝公共索引增刪改查
索引是Elasticsearch中存儲(chǔ)數(shù)據(jù)的邏輯單元,類似于關(guān)系數(shù)據(jù)庫(kù)中的表,它包含多個(gè)文檔,每個(gè)文檔都是一個(gè)結(jié)構(gòu)化的JSON數(shù)據(jù)格式,在實(shí)際應(yīng)用中,索引的使用與配置可以依據(jù)不同的方案進(jìn)行,例如在Spring Boot項(xiàng)目中,可以選擇自動(dòng)配置或者手動(dòng)編寫配置類2024-10-10

