public class Test {
public static void main(String[] argv) {
String str = "A,B,C,D,E";
String[] arr = str.split(",");
String result = "";
for (int i = 0; i < arr.length; i++) {
for (int j = 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
String str="A,B,C,D,E";
String[] strarr=str.split(",");
String result="";
for(int i=0;i
for(int j=i+1;jif(j>1)result+=",";
String r=strarr[i]+strarr[j];
result+=r;
}
}
string str = "A,B,C,D,E";
string[] strs = str.Split(',');
string result = "";
for (int i = 0; i < strs.Length; i++)
{
for (int j = i+1; j < strs.Length; j++)
{
result += strs[i] + strs[j] + ",";
}
}
解析字符串,逗号分隔,存到数组中,两层for 循环相加。
import java.util.Arrays;
public class answer1
{
public static void main(String[] args)
{
String str = "A,B,C,D,E";
String[] array = str.split(",");//以逗号去分割使他成为数组
System.out.println(Arrays.toString(array));
for(int i=0;i
for(int j=i+1;j
System.out.println(array[i] + array[j]);
}
}
}
}