久久久久无码精品,四川省少妇一级毛片,老老熟妇xxxxhd,人妻无码少妇一区二区

Java常用字符串反轉(zhuǎn)的五種方案

時(shí)間:2024-05-06 15:47:20 JAVA認(rèn)證 我要投稿
  • 相關(guān)推薦

Java常用字符串反轉(zhuǎn)的五種方案

  java程序中如何一個(gè)字符串進(jìn)行反轉(zhuǎn)?或者在字符串中指定部分進(jìn)行反轉(zhuǎn)的方法有哪些?為幫助大家解決這個(gè)疑問(wèn),yjbys小編為同學(xué)們分享最新Java常用字符串反轉(zhuǎn)的五種方案,快來(lái)看看吧!

Java常用字符串反轉(zhuǎn)的五種方案

  Java常用字符串反轉(zhuǎn)的5種方案,代碼如下:

  第一個(gè)類(lèi)是運(yùn)行類(lèi)Run.java

  package com.imooc.strreverse;

  import java.util.Scanner;

  public class Run {

  public Run() {

  System.out.print("請(qǐng)輸入一個(gè)字符串:");

  Scanner sc = new Scanner(System.in);

  String str = sc.next();

  System.out.println();

  String reversedStrByArray = new Reverse(str).reverseByArray();

  System.out.println("對(duì)應(yīng)反轉(zhuǎn)字符串為[Array]:"+ reversedStrByArray);

  String reversedStrByStack = new Reverse(str).reverseByStack();

  System.out.println("對(duì)應(yīng)反轉(zhuǎn)字符串為[Stack]:"+ reversedStrByStack);

  String reversedStrBySort = new Reverse(str).reverseBySort();

  System.out.println("對(duì)應(yīng)反轉(zhuǎn)字符串為[逆序遍歷]:"+ reversedStrBySort);

  String reversedStrByBit = new Reverse(str).reverseByBit();

  System.out.println("對(duì)應(yīng)反轉(zhuǎn)字符串為[位運(yùn)算]:"+ reversedStrByBit);

  String reversedStrByRecursive = new Reverse(str).reverseByRecursive(str);

  System.out.println("對(duì)應(yīng)反轉(zhuǎn)字符串為[遞歸]:"+ reversedStrByRecursive); }

  public static void main(String[] args) {

  new Run();

  }

  }

  第二段代碼是實(shí)現(xiàn)類(lèi)Reverse.java

  package com.imooc.strreverse;

  import java.util.Stack;

  public class Reverse {

  String str = new String();

  public Reverse(String str) {

  this.str = str;

  }

  //用數(shù)組實(shí)現(xiàn)

  public String reverseByArray() {

  if(str == null str.length() == 0) {

  return str;

  }

  int len = str.length();

  char[] chArray = str.toCharArray();

  for(int i= 0; i< len/2; i++) {

  char temp;

  temp = chArray[i];

  chArray[i] = chArray[len- 1- i];

  chArray[len- 1- i] = temp;

  }

  return new String(chArray);

  }

  //用棧實(shí)現(xiàn)

  public String reverseByStack() {

  if(str == null str.length() == 0) {

  return str;

  }

  Stack strStack = new Stack();

  char[] chArray = str.toCharArray();

  for(Character ch: chArray) {

  strStack.push(ch);

  }

  int len = str.length();

  for(int i= 0; i< len; i++) { chArray[i] = strStack.pop();

  }

  return new String(chArray);

  }

  //用逆序遍歷實(shí)現(xiàn)

  public String reverseBySort() {

  if(str == null str.length() == 0) {

  return str;

  }

  StringBuilder sb = new StringBuilder();

  for(int i= str.length()- 1; i>= 0; i--) {

  sb.append(str.charAt(i));

  }

  return sb.toString();

  }

  //用位運(yùn)算實(shí)現(xiàn)

  public String reverseByBit() {

  if(str == null str.length() == 0) {

  return str;

  }

  char[] chArray = str.toCharArray();

  int len = str.length();

  for(int i= 0; i< len/ 2; i++) {

  chArray[i]^= chArray[len- 1- i];

  chArray[len- 1- i]^= chArray[i];

  chArray[i]^= chArray[len- 1- i];

  }

  return new String(chArray);

  }

  //用遞歸實(shí)現(xiàn)

  public String reverseByRecursive(String str) {

  if(str == null str.length() == 0) {

  return str;

  }

  int len = str.length();

  if(len == 1) {

  return str;

  } else {

  return reverseByRecursive(str.substring(1))+ str.charAt(0);

  }

  }

  }

【Java常用字符串反轉(zhuǎn)的五種方案】相關(guān)文章:

關(guān)于JAVA字符串的拼接與性能06-07

Java認(rèn)證基礎(chǔ)知識(shí):java字符串轉(zhuǎn)化整型問(wèn)題07-21

sun認(rèn)證java關(guān)于字符串處理技巧06-01

JAVA十六進(jìn)制與字符串的轉(zhuǎn)換方法09-17

反轉(zhuǎn)壽司的做法08-01

javascript字符串對(duì)象常用api函數(shù)小結(jié)06-22

反轉(zhuǎn)壽司的制作做法07-10

Java枚舉的常用方法10-05

Java數(shù)組定義常用方法09-28

php開(kāi)發(fā)中常用的操作字符串的函數(shù)說(shuō)明07-19