java字元串組合
Ⅰ java程序如何實現對字元串的排列組合問題
import java.math.BigInteger;
import java.util.*;
public class PermutationGenerator {
private int[] a;
private BigInteger numLeft;
private BigInteger total;
public PermutationGenerator(int n) {
if (n < 1) {
throw new IllegalArgumentException("Min 1");
}
a = new int[n];
total = getFactorial(n);
reset();
}
public void reset() {
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
numLeft = new BigInteger(total.toString());
}
public BigInteger getNumLeft() {
return numLeft;
}
public BigInteger getTotal() {
return total;
}
public boolean hasMore() {
return numLeft.compareTo(BigInteger.ZERO) == 1;
}
private static BigInteger getFactorial(int n) {
BigInteger fact = BigInteger.ONE;
for (int i = n; i > 1; i--) {
fact = fact.multiply(new BigInteger(Integer.toString(i)));
}
return fact;
}
public int[] getNext() {
if (numLeft.equals(total)) {
numLeft = numLeft.subtract(BigInteger.ONE);
return a;
}
int temp;
// Find largest index j with a[j] < a[j+1]
int j = a.length - 2;
while (a[j] > a[j + 1]) {
j--;
}
// Find index k such that a[k] is smallest integer
// greater than a[j] to the right of a[j]
int k = a.length - 1;
while (a[j] > a[k]) {
k--;
}
// Interchange a[j] and a[k]
temp = a[k];
a[k] = a[j];
a[j] = temp;
// Put tail end of permutation after jth position in increasing order
int r = a.length - 1;
int s = j + 1;
while (r > s) {
temp = a[s];
a[s] = a[r];
a[r] = temp;
r--;
s++;
}
numLeft = numLeft.subtract(BigInteger.ONE);
return a;
}
//程序測試入口
public static void main(String[] args) {
int[] indices;
String[] elements = { "a", "b", "c"};
PermutationGenerator x = new PermutationGenerator(elements.length);
StringBuffer permutation;
while (x.hasMore())
{
permutation = new StringBuffer("%");
indices = x.getNext();
for (int i = 0; i < indices.length; i++) {
permutation.append(elements[indices[i]]).append("%");
}
System.out.println(permutation.toString());
}
}
}
先給你一個看看!
Ⅱ java怎麼把2個字元串拼接在一起
String類的方法:
①利用運算符"+"
②public String concat(String str)進行字元串的拼接操作
StringBuffer的方法:
①public StringBuffer append(String str)將str添加到當前字元串緩沖區的字元序列的末尾
②public StringBuffer insert(int offset,String str)在當前字元串緩沖區的字元序列的下標
索引offset插入str。如果offset等於舊長度,則str添加在字元串緩沖區的尾部
如圖所示
Ⅲ java字元串合並
publicclassTest{
publicstaticvoidmain(String[]args){
Stringstr="0123456";
Stringresult="";
intid=3;
for(inti=1;i<=id;i++){
result+=str+i;
}
System.out.println(result);
}
}
Ⅳ 在JAVA語言中怎麼樣合並字元串
publicstaticvoidmain(String[]args){
Strings1="abc";
Strings2="123";
//1
System.out.println(s1.concat(s2));
//2
System.out.println(String.format("%s%s",s1,s2));
//3
StringBuildersbd=newStringBuilder();
sbd.append(s1);
sbd.append(s2);
System.out.println(sbd.toString());
}
//別問我為什麼不用s1+s2,因為我們項目里,誰用加號連接字元串就乾死誰。
Ⅳ java字元串合並的問題
字元串值應該用equals比較相等, ==比較的是對象不是值
Ⅵ java 列出一個字元串的全字元組合情況,不考慮重復字元
package;
importjava.util.Arrays;
importjava.util.LinkedList;
publicclassRecursionSub3Sort
{
staticintcount=0;
staticchar[]array={'a','b','c'};
staticLinkedList<char[]>list=newLinkedList<char[]>();
staticint[]indexs=newint[3];
staticintlen=array.length;
publicstaticvoidmain(String[]args)
{
getSub();
for(char[]cs:list)
{
System.out.println(Arrays.toString(cs));
}
}
privatestaticLinkedList<char[]>getSub()
{
while(count<=len)
{
recursionSub(0,-1);
count++;
}
returnlist;
}
privatestaticLinkedList<char[]>recursionSub(intind,intstart)
{
start++;
if(start>count-1)
{
returnnull;
}
for(indexs[start]=0;indexs[start]<len;indexs[start]++)
{
recursionSub(0,start);
if(start==count-1)
{
char[]temp=newchar[count];
for(inti=count-1;i>=0;i--)
{
temp[start-i]=array[indexs[start-i]];
}
if(temp.length==1
||Arrays.toString(temp).replaceAll("[\[\]\s,]","").replaceAll("(\w)\1+","$1")
.length()!=1)
{
list.add(temp);
}
}
}
returnlist;
}
}
Ⅶ java將字元串隨機打亂並且可以重新組合的方法
Scanner scanner = new Scanner(System.in);
System.out.print("輸入字元串:");
String str = scanner.nextLine();
List<Map> mapList = new ArrayList<>();
int length = str.length();
for(int i = 0;i < length;i++){
int x = (int)(Math.random()*length);
Map map = new HashMap();
map.put("sort",x);
map.put("value",str.charAt(i));
mapList.add(map);
}
System.out.print("輸出隨機變換後的結果:");
mapList.stream()
.sorted(Comparator.comparing(o -> o.get("sort").toString()))
.forEach(x-> System.out.print(x.get("value").toString()));
System.out.println();
Ⅷ java字元串數組合並 怎麼合並成一個數組
java字元串數組合並,可以使用array.復制方法,如下代碼:
packagecom.qiu.lin.he;
importjava.text.ParseException;
importjava.util.Arrays;
publicclassCeshi{
publicstaticvoidmain(String[]args)throwsParseException{
String[]str1={"J","a","v","a","中"};
String[]str2={"如","何","把","兩","個","數","組","合","並","為",
"一","個"};
intstrLen1=str1.length;//保存第一個數組長度
intstrLen2=str2.length;//保存第二個數組長度
str1=Arrays.Of(str1,strLen1+strLen2);//擴容
System.array(str2,0,str1,strLen1,strLen2);//將第二個數組與第一個數組合並
System.out.println(Arrays.toString(str1));//輸出數組
}
}
運行結果如下:
Ⅸ Java 字元串解析,任意組合。
publicclassTest{
publicstaticvoidmain(String[]argv){
Stringstr="A,B,C,D,E";
String[]arr=str.split(",");
Stringresult="";
for(inti=0;i<arr.length;i++){
for(intj=i+1;j<arr.length;j++){
if(i<arr.length-2){
result+=arr[i]+arr[j]+",";
}else{
result+=arr[i]+arr[j];
}
}
}
System.out.println(result);
}
}
//輸出結果:
AB,AC,AD,AE,BC,BD,BE,CD,CE,DE
Ⅹ 如何在java里java字元串數組合並成一個數組
具體如下:
java字元串數組合並,可以使用array.復制方法,如下代碼:
package com.qiu.lin.he;
import java.text.ParseException;
import java.util.Arrays;
public class Ceshi {
public static void main(String[] args) throws ParseException {
String[] str1 = { "J", "a", "v", "a", "中" };
String[] str2 = { "如", "何", "把", "兩", "個", "數", "組", "合", "並", "為",
"一", "個" };
int strLen1 = str1.length;// 保存第一個數組長度
int strLen2 = str2.length;// 保存第二個數組長度
str1 = Arrays.Of(str1, strLen1 + strLen2);// 擴容
System.array(str2, 0, str1, strLen1, strLen2);// 將第二個數組與第一個數組合並
System.out.println(Arrays.toString(str1));// 輸出數組
}
}