Java 字符串组合函数不使用';I don’我没有按预期工作
给定两个字符串s1和s2作为输入,创建一个由s1的第一个字符、s2的第一个字符、s1的第二个字符和s2的第二个字符组成的字符串,依此类推。任何剩余的字符都会出现在结果字符串的末尾 我的方法 为了组合str1的第一个字符串和str2的另一个字符串,我从str1和str2中提取每个字符并尝试附加它。当字符串长度相等时,我得到了正确的输出。但是字符串1的长度小于字符串2的长度,我得到了错误的输出Java 字符串组合函数不使用';I don’我没有按预期工作,java,string,Java,String,给定两个字符串s1和s2作为输入,创建一个由s1的第一个字符、s2的第一个字符、s1的第二个字符和s2的第二个字符组成的字符串,依此类推。任何剩余的字符都会出现在结果字符串的末尾 我的方法 为了组合str1的第一个字符串和str2的另一个字符串,我从str1和str2中提取每个字符并尝试附加它。当字符串长度相等时,我得到了正确的输出。但是字符串1的长度小于字符串2的长度,我得到了错误的输出 public String combine(String str1,String str2) {
public String combine(String str1,String str2)
{
int l1=str1.length();
int l2=str2.length();
String strnew="";
if(l1>=l2)
{
for(int j=0;j<l1;j++)
{
char c1=str1.charAt(j);
strnew=strnew+c1;
for(int p=j;p<=j && p<l2 ;p++) @Edit
{
char c2=str2.charAt(p);
strnew=strnew+c2;
}
}
}
else
{
for(int j=0;j<l2;j++)
{
char c1=str1.charAt(j);
strnew=strnew+c1;
for(int p=j;p<=j && p<l1;p++) @Edit
{
char c2=str2.charAt(p);
strnew=strnew+c2;
}
}
}
return strnew;
}
不需要使用嵌套循环。您可以使用这样的单个循环
public static String combine(String str1,String str2) {
String output = "";
// Loop as long as i < as str1.length or str2.length
for(int i = 0; i<str1.length() || i < str2.length(); ++i) {
if(i<str1.length()) { // add the char at i to the ouput if there is a char left to take
output += str1.charAt(i);
}
if(i<str2.length()) { // add the char at i to the ouput if there is a char left to take
output += str2.charAt(i);
}
}
return output;
}
正如芭丝谢芭所说,这里最好不要使用字符串,而可以使用StringBuilder
public static String combine(String str1,String str2) {
//Use the complete length as initial capizity
StringBuilder output = new StringBuilder(str1.length()+str2.length());
for(int i = 0; i<str1.length() || i < str2.length(); ++i) {
if(i<str1.length()) {
output.append(str1.charAt(i));
}
if(i<str2.length()) {
output.append(str2.charAt(i));
}
}
return output.toString();
}
公共静态字符串组合(字符串str1、字符串str2){
//使用完整长度作为初始容量
StringBuilder输出=新的StringBuilder(str1.length()+str2.length());
对于(inti=0;i您可以使用不同的方法;将字符串转换为char[],如下所示
char [] arrstr1 = str1.toCharArray();
然后选择较小的数组,并在其上迭代,每次选择每个数组的第i个字符。
然后,添加较大字符串的剩余字符
编辑:添加更多代码以更好地解释
char [] arrstr1 = str1.toCharArray();
char [] arrstr2 = str2.toCharArray();
String res="";
int i=0;
for(; i<arrstr1.length || i<arrstr2.length ; i++){
res+=arrstr1[i]+arrstr2[i];
}
if(arrstr1.length >i) res+= str1.substring(i);
if(arrstr2.length >i) res+= str2.substring(i);
char[]arrstr1=str1.tocharray();
char[]arrstr2=str2.toCharArray();
字符串res=“”;
int i=0;
对于(;ii)res+=str2.子串(i);
性能不是很好,但应该可以工作。问题是当一个字符串比另一个字符串长时,不能将内部循环绑定到该长度。
除了已经设置的停止条件外,还可以通过在嵌套循环中添加p
和p
来更正它
public static String combine(String str1,String str2)
{
int l1=str1.length();
int l2=str2.length();
String strnew="";
if(l1>=l2)
{
for(int j=0;j<l1;j++)
{
char c1=str1.charAt(j);
strnew=strnew+c1;
for(int p=j;p<=j && p<l2;p++)
{
char c2=str2.charAt(p);
strnew=strnew+c2;
}
}
}
else
{
for(int j=0;j<l2;j++)
{
char c1=str1.charAt(j);
strnew=strnew+c1;
for(int p=j;p<=j && p < l1;p++)
{
char c2=str2.charAt(p);
strnew=strnew+c2;
}
}
}
return strnew;
}
}
公共静态字符串组合(字符串str1、字符串str2)
{
int l1=str1.length();
int l2=str2.length();
字符串strnew=“”;
如果(l1>=l2)
{
对于(int j=0;j这是我能想到的最清晰的方法。我喜欢将“尾字符串”添加为常规算法的一部分。请注意空检查和使用StringBuilder
:
String combine(String s1, String s2)
{
if (s1 == null && s2 == null){
return null;
} else if (s1 == null){
return s2;
} else if (s2 == null){
return s1;
} else {
StringBuilder sb = new StringBuilder();
for (int pos = 0; pos < Math.max(s1.length(), s2.length()); ++pos){
if (pos < s1.length()){
sb.append(s1.charAt(pos));
}
if (pos < s2.length()){
sb.append(s2.charAt(pos));
}
}
return sb.toString();
}
}
字符串组合(字符串s1、字符串s2)
{
如果(s1==null&&s2==null){
返回null;
}else if(s1==null){
返回s2;
}else if(s2==null){
返回s1;
}否则{
StringBuilder sb=新的StringBuilder();
对于(int pos=0;pos
我认为,由于字符串不可变,对max
的重复调用将得到优化。但是,它仍然会进行一些不必要的长度检查。另一种方法是使用巧妙的wierd for循环:
public static String combine(String str1,String str2) {
StringBuilder result = new StringBuilder();
int str1size = str1 == null ? 0 : str1.length();
int str2size = str2 == null ? 0 : str2.length();
int lowerSize = Math.min(str1size, str2size);
int greaterSize = Math.max(str1size, str2size);
int i; // define the counter variable outside of a for, because we will reuse it in the following for loops
for (i=0; i < lowerSize; i++) { // browse the common part of the strings
result.append(str1.charAt(i)).append(str2.charAt(i));
}
for (int upTo = Math.min(greaterSize, str1size); i < upTo ; i++) { // browse the remaining part of str1, if applicable
result.append(str1.charAt(i));
}
for (int upTo = Math.min(greaterSize, str2size); i < upTo; i++) { // browse the remaining part of str2, if applicable
result.append(str2.charAt(i));
}
return result.toString();
}
公共静态字符串组合(字符串str1、字符串str2){
StringBuilder结果=新建StringBuilder();
int str1size=str1==null?0:str1.length();
int str2size=str2==null?0:str2.length();
int lowerSize=Math.min(str1size,str2size);
int greaterSize=Math.max(str1size,str2size);
int i;//在for之外定义计数器变量,因为我们将在下面的for循环中重用它
对于(i=0;i
你可以试试。我想你可能想试一试
import java.util.Scanner;
public class WordCombiner {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String firstWord="", secondWord="", remainingLetters="", newWord = "";
System.out.print("enter first word: ");
firstWord = scan.nextLine();
System.out.print("enter second word: ");
secondWord = scan.nextLine();
int firstWordLength = firstWord.length();
int secondWordLength = secondWord.length();
if(firstWordLength==secondWordLength){
newWord = combine(firstWord,secondWord);
}else if(firstWordLength>secondWordLength){
remainingLetters= firstWord.substring(secondWordLength, firstWordLength);
firstWord = firstWord.substring(0,firstWordLength-remainingLetters.length());
newWord = combine(firstWord, secondWord,remainingLetters);
}else{
remainingLetters = secondWord.substring(firstWordLength,secondWordLength);
secondWord = secondWord.substring(0,secondWordLength-remainingLetters.length());
newWord = combine(firstWord, secondWord,remainingLetters);
}
System.out.print("combined word: "+ newWord);
}
private static String combine(String firstWord, String secondWord,
String remainingLetters) {
// TODO Auto-generated method stub
return combine(firstWord, secondWord)+remainingLetters;
}
private static String combine(String firstWord, String secondWord) {
String word = "";
for(int i = 0; i<firstWord.length();i++){
word+=firstWord.charAt(i);
word+=secondWord.charAt(i);
}
return word;
}
}
然后使用三个参数(第一个字、第二个字、剩余的单个词)调用combine
使用三个参数的组合方法的工作原理与使用两个参数的组合方法几乎相同,只是我们在末尾添加剩余的字母。使用StringBuilder
构建结果字符串。用于(int p=j;p此代码实际上为失败输出NOTNULL引发了一个异常!标题是什么意思?请修复它。它将有助于未来的搜索。不要在所有这些连接中使用字符串。请漂亮。请不要只是转储代码:解释您所做的更改。糟糕,当我收到您的评论时,我只是在编辑。是否足够清楚现在?@PaulK。请更正代码。我上一个测试用例搞错了。除了我用过的以外,我不能使用任何内置函数。
public static String combine(String str1,String str2) {
StringBuilder result = new StringBuilder();
int str1size = str1 == null ? 0 : str1.length();
int str2size = str2 == null ? 0 : str2.length();
int lowerSize = Math.min(str1size, str2size);
int greaterSize = Math.max(str1size, str2size);
int i; // define the counter variable outside of a for, because we will reuse it in the following for loops
for (i=0; i < lowerSize; i++) { // browse the common part of the strings
result.append(str1.charAt(i)).append(str2.charAt(i));
}
for (int upTo = Math.min(greaterSize, str1size); i < upTo ; i++) { // browse the remaining part of str1, if applicable
result.append(str1.charAt(i));
}
for (int upTo = Math.min(greaterSize, str2size); i < upTo; i++) { // browse the remaining part of str2, if applicable
result.append(str2.charAt(i));
}
return result.toString();
}
import java.util.Scanner;
public class WordCombiner {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String firstWord="", secondWord="", remainingLetters="", newWord = "";
System.out.print("enter first word: ");
firstWord = scan.nextLine();
System.out.print("enter second word: ");
secondWord = scan.nextLine();
int firstWordLength = firstWord.length();
int secondWordLength = secondWord.length();
if(firstWordLength==secondWordLength){
newWord = combine(firstWord,secondWord);
}else if(firstWordLength>secondWordLength){
remainingLetters= firstWord.substring(secondWordLength, firstWordLength);
firstWord = firstWord.substring(0,firstWordLength-remainingLetters.length());
newWord = combine(firstWord, secondWord,remainingLetters);
}else{
remainingLetters = secondWord.substring(firstWordLength,secondWordLength);
secondWord = secondWord.substring(0,secondWordLength-remainingLetters.length());
newWord = combine(firstWord, secondWord,remainingLetters);
}
System.out.print("combined word: "+ newWord);
}
private static String combine(String firstWord, String secondWord,
String remainingLetters) {
// TODO Auto-generated method stub
return combine(firstWord, secondWord)+remainingLetters;
}
private static String combine(String firstWord, String secondWord) {
String word = "";
for(int i = 0; i<firstWord.length();i++){
word+=firstWord.charAt(i);
word+=secondWord.charAt(i);
}
return word;
}
}
firstWord = tie
secondWord = rsc
remainingLetters = t