我正在尝试让这个程序的groupPairs函数在初始String数组[One,Two,Three,Four,Five,Six]中获取六个字符串并创建一个大小为一半的新String字符串(3)原始的六个字符串按顺序成对分组[OneTwo,ThreeFour,FiveSix],然后将生成的新String []返回到main方法。
import java.util.*;
public class Application
{
static String[] groupPairs(String[] array)
{
String[] newArray = new String[(array.length)/2];
int count=0;
for(String string:newArray)
{
newArray[count]=array[count].append(array[count+1]);
count=count+2;
}
return newArray;
}
public static void main(String args[]) //main method, don't worry about this
{
String[] list = new String[5];
list[0]="One";
list[1]="Two";
list[2]="Three";
list[3]="Four";
list[4]="Five";
list[5]="Six";
String[] list2 = groupPairs(list);
}
}
尝试编译程序时,出现此错误:
Application.java:11: cannot find symbol
symbol : method append(java.lang.String)
location: class java.lang.String
newArray[count]=array[count].append(array[count+1]);
^
有关如何修复此行的任何想法,以便我的新数组将包含原始String []值的连接对,将非常感谢!
答案 0 :(得分:0)
您无法对阵列执行追加操作。请尝试以下方法。
String[] list = new String[6];
list[0] = "One";
list[1] = "Two";
list[2] = "Three";
list[3] = "Four";
list[4] = "Five";
list[5] = "Six";
String[] list2 = new String[list.length / 2];
for (int i = 0, j = 0; i < list.length; i++, j++)
{
list2[j] = list[i] + list[++i];
}