在处理中将问题附加到数组

时间:2017-03-21 20:58:24

标签: java arrays append processing

我正在尝试通过串口发送数组。这个数组的名称是'send',最初它是空的。我想将整数附加到此数组,但我遇到了问题。我打印数组,但它什么都没打印出来。有人可以帮助我吗?

谢谢!

if (filesAreSame)
{
    MessageBox.Show("They match!");
}
else
{
    var commonItems = oldFileTxt.Intersect(newFileTxt, StringComparer.OrdinalIgnoreCase);
    var uniqueOldItems = oldFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);
    var uniqueNewItems = newFileTxt.Except(commonItems, StringComparer.OrdinalIgnoreCase);

    var notEqualsFileText = new StringBuilder();
    if (uniqueOldItems.Any())
    {
        notEqualsFileText.AppendLine(
            $"Entries in {oldFilePath} that are not in {newFilePath}:");
        notEqualsFileText.AppendLine(string.Join(Environment.NewLine, uniqueOldItems));
    }
    if (uniqueNewItems.Any())
    {
        notEqualsFileText.AppendLine(
            $"Entries in {newFilePath} that are not in {oldFilePath}:");
        notEqualsFileText.AppendLine(string.Join(Environment.NewLine, uniqueNewItems));
    }

    File.WriteAllText(notEqualFilePath, notEqualsFileText.ToString());

    var equalsFileText = new StringBuilder();
    if (commonItems.Any())
    {
        equalsFileText.AppendLine(
            $"Entries that are common in both {newFilePath} and {oldFilePath}:");
        equalsFileText.AppendLine(string.Join(Environment.NewLine, commonItems));
    }
    else
    {
        equalsFileText.AppendLine(
            $"There are no common entries in both {newFilePath} and {oldFilePath}.");
    }

    File.WriteAllText(equalFilePath, equalsFileText.ToString());

    MessageBox.Show("The files are not the same! Please look at 'Not Equal.txt' to see the difference. Look at 'Equal.txt' to see what is the same at this time.");
}

2 个答案:

答案 0 :(得分:1)

根据reference,您应该保存函数调用的结果:

send = append(send, 1);

答案 1 :(得分:0)

就我而言,我需要创建一个对象数组,该对象可以动态增长,并且唯一可行的方法是使用 ArrayList ,这是完美的:

  ArrayList<MyObject> myObjects = new ArrayList<MyObject>();

只有那时,我才能够使用.add和.remove动态调整数组大小。

添加对象:

  objTemp = new MyObject(x, y);
  myObjects.add(objTemp);

删除所有对象:

  for (int i = myObjects.size() - 1; i >= 0; i--) {
    myObjects.remove(i);
  }