【发布时间】:2015-04-23 07:28:36
【问题描述】:
我们正在尝试比较两个字符串数组(as[] 和 bs[])并使用 bs[] 中存在的新字符串更新数组字符串 as[]。我们无法更新 as[].Pls帮助我们使用以下代码。谢谢你;)
public class Aa {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create an array of 4 strings (indexes 0 - 3)
String as[] = new String[5];
String bs[] = new String[16];
int i;
try {
// Create a bufferreader object to read our file with.
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
BufferedReader reader1;
reader1 = new BufferedReader(new FileReader("a1.txt"));
// Line will hold our line read from the file
String line = "";
String line1 = "";
// The counter will keep track of how many lines we have read
int counter = 0;
int counter1 = 0;
// Read in a line from the file and store it in "line". Do this while we don't hit null or while the counter is less than 4.
// The counter prevents us from reading in too many lines.
while (((line = reader.readLine()) != null) && (counter < 4)) {
as[counter] = line;
counter++;
}
while (((line1 = reader1.readLine()) != null) && (counter1 < 16)) {
bs[counter1] = line1;
counter1++;
}
System.out.println("value"+as[0]);
System.out.println("value"+bs[0]);
int temp,temp1,j;
temp=as.length;
temp1=bs.length;
System.out.println("length:"+temp);
System.out.println("length1:"+temp1);
for(i=0;i<bs.length;i++)
{
for(j=0;j<as.length;j++)
{
if(as[j].equals(bs[i]))
{
//ignore
}
else
{
temp++;
as[temp]=bs[i];
}
}
}
// With a foreach style loop we loop through the array of strings and print them out to show they were read in.
reader1.close();
reader.close();
}
catch (Exception ex) { System.out.println("Exception: " + ex.getMessage()); }
}
}
【问题讨论】:
-
你不清楚,
update the array string as[ ] with the new strings present in bs[ ]是什么意思 -
如果您尝试追加到数组,这在 Java 中不起作用。数组是固定长度的。
-
// Create an array of 4 strings (indexes 0 - 3): 别忘了更新你的 cmets