1

我正在从 USB 驱动器将文本文件读入 Android 4.0 平板电脑,其中许多行以选项卡开头。作为语言的测验实用程序,典型的行是

[tab]an experience[crlf]

在拆分时,我有一个空数组元素,到目前为止我只能通过测试 if 来检测stringJustSplit[0].length( ) == 0。这并不安全,因为长度是元素的属性而不是它的属性。当然==""== null并没有让我到任何地方。

我看到了这个RemoveEmptyEntries选项,但我宁愿保留空元素,并且程序会有意地响应它们。

是否有声明可以处理它们?

日志产生:

    ---Array trial---(6229): Start
    ---Array trial---(6229): Located by length( )==0.
    ---Array trial---(6229): End

从这个方法:

while ((Fileline = buffered_reader.readLine( )) != null) {
    line2array = Fileline.split("\\t");
    String[] tester = new String[1];
    String dummyStr = "|element1";
    String[] emptyField = dummyStr.split("\\|");
    if (true)                         {Log.v(T, "Start results");}
    if (line2array[0] == emptyField[0]){Log.v(T, "Located by array element created similar way.");}
    if (line2array[0] == tester[0])   {Log.v(T, "Located by comparison w just-made array.");}
    if (line2array[0].length( ) == 0) {Log.v(T, "Located by length( ) == 0.");}
    if (line2array[0] == "")          {Log.v(T, "Located by zero-len string.");}
    if (line2array[0] == null)        {Log.v(T, "Located by null.");}
    if (true)                         {Log.v(T, "End");}
    }
4

1 回答 1

0

在Java中,需要equals比较字符串内容的方法,==会比较变量是否引用了内存中的同一个地址。

所以你的代码是:

// can't call .equals on something that is null
if (line2array[0] != null && line2array[0].equals(emptyField[0])) {
  Log.v(T, "Located by array element created similar way.");
}
if (line2array[0] != null && line2array[0].equals(tester[0])) {
  Log.v(T, "Located by comparison w just-made array.");
}
if (line2array[0].length() == 0) {
  Log.v(T, "Located by length( ) == 0.");
}
// literals are never null
if ("".equals(line2array[0])) {
  Log.v(T, "Located by zero-len string.");
}
if (line2array[0] == null) {
  Log.v(T, "Located by null.");
}
if (true) {
  Log.v(T, "End");
}
于 2012-07-02T15:33:59.367 回答