当前位置:首页 » 编程语言 » java字符串组合

java字符串组合

发布时间: 2022-07-02 08:55:07

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));// 输出数组

}
}

热点内容
512缓存录音爆音 发布:2024-05-01 08:11:59 浏览:667
三上悠亚校花下载ftp 发布:2024-05-01 08:01:05 浏览:790
printpython不换行 发布:2024-05-01 08:00:28 浏览:739
笔记本电脑弹出服务器ip 发布:2024-05-01 07:53:11 浏览:268
nuget服务器搭建 发布:2024-05-01 07:40:20 浏览:333
如何修改球球英雄密码 发布:2024-05-01 07:20:29 浏览:228
aspnet防止反编译 发布:2024-05-01 07:14:51 浏览:156
纯净的安卓系统哪个版本最流畅 发布:2024-05-01 07:05:50 浏览:787
光纤和密码忘记了怎么办 发布:2024-05-01 06:57:23 浏览:783
搭建服务器的路由器 发布:2024-05-01 06:51:12 浏览:585