我有一个java程序,其中以下是我想要实现的目标:
first input: ABC
second input: xyz
output: AxByCz
我的Java程序如下:
import java.io.*;
class DisplayStringAlternately
{
public static void main(String[] arguments)
{
String firstC[], secondC[];
firstC = new String[] {"A","B","C"};
secondC = new String[] {"x","y","z"};
displayStringAlternately(firstC, secondC);
}
public static void displayStringAlternately (String[] firstString, String[] secondString)
{
int combinedLengthOfStrings = firstString.length + secondString.length;
for(int counter = 1, i = 0; i < combinedLengthOfStrings; counter++, i++)
{
if(counter % 2 == 0)
{
System.out.print(secondString[i]);
}
else
{
System.out.print(firstString[i]);
}
}
}
}
但是我遇到了以下运行时错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
AyC at DisplayStringAlternately.displayStringAlternately(DisplayStringAlternately.java:23)
at DisplayStringAlternately.main(DisplayStringAlternately.java:12)
Java Result: 1
我的Java程序有什么错误?
答案 0 :(得分:2)
正如您所评论的,如果两个数组长度相同,您只需执行
即可 firstC = new String[] {"A","B","C"};
secondC = new String[] {"x","y","z"};
然后
for(int i = 0; i < firstC.length; i++) {
System.out.print(firstC[i]);
System.out.print(secondC[i]);
}
答案 1 :(得分:2)
如果两个数组具有相同的长度for
,则应在i < anyArray.length
时继续循环。
此外,您不需要任何counter
来确定应首先打印哪个阵列。只需硬编码将从firstString
打印第一个元素,然后从secondString
打印下一个元素。
因此,您的displayStringAlternately
方法可能看起来像
public static void displayStringAlternately(String[] firstString,
String[] secondString) {
for (int i = 0; i < firstString.length; i++) {
System.out.print(firstString[i]);
System.out.print(secondString[i]);
}
}
无论如何,你的代码抛出ArrayIndexOutOfBoundsException
,因为每次你决定从哪个数组打印元素递增i
时,你都会以这种方式跳过数组
i=0 i=2
{"A","B","C"};
{"x","y","z"};
i=1 i=3
^^^-here is the problem
所以你看到你的代码试图从第二个数组中访问不在其中的元素(它超出了它的范围)。
答案 2 :(得分:0)
使用字符串的组合长度是错误的,例如,当{i> = secondString.length时,secondString[i]
会导致异常。
答案 3 :(得分:0)
尝试使用高性能的以下工作代码
public static void main(String[] arguments)
{
String firstC[], secondC[];
firstC = new String[] {"A","B","C"};
secondC = new String[] {"x","y","z"};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < firstC.length; i++) {
builder.append(firstC[i]);
builder.append(secondC[i]);
}
System.out.println(builder.toString());
}
答案 4 :(得分:0)
public class concad {
public void main(String[] args) {
String s1 = "RAMESH";
String s2 = "SURESH";
int i;
int j;
for (i = 0; i < s1.length(); i++) {
System.out.print(s1.charAt(i));
for (j = i; j <= i; j++) {
if (j == i) {
System.out.print(s2.charAt(j));
}
}
}
}
}
答案 5 :(得分:0)
我已经提到了两个字符串。然后在内部for循环中传递一个计数器变量和第二个字符串,然后为每个偶数位置传递代码&#34;计数器%2&#34;。如果有任何问题,请检查然后在下面评论。
public class AlternatePosition {
public static void main(String[] arguments) {
String abc = "abcd";
String def = "efgh";
displayStringAlternately(abc, def);
}
public static void displayStringAlternately(String firstString, String secondString) {
for (int i = 0; i < firstString.length(); i++) {
for (int counter = 1, j = 0; j < secondString.length(); counter++, j++) {
if (counter % 2 == 0) {
System.out.print(secondString.charAt(i));
break;
} else {
System.out.print(firstString.charAt(i));
}
}
}
}
}