P5:删除包含 Null/NaN 的 TableRows
P5: Remove TableRows containing Null/NaN
目标是 return 一个干净的 table,它只包含指定列中包含数字数据的行。
下面的代码对我有用。但我忍不住觉得有更好的方法来做到这一点。关于更优雅的解决方案的任何想法?
我有代码:
Table removeEmptyRows(Table data, String column_name)
{
IntList rows_to_remove = new IntList();
Table dataCopy = cloneTable(data);
for (int r = 0; r<dataCopy.getRowCount(); r++)
{
String value_string = dataCopy.getString(r, column_name);
///filter out the NaNs
if ( ! isNullOrBlank(value_string))
{
if ( ! isNumeric(value_string) )
{
rows_to_remove.append(r);
}
} else {
rows_to_remove.append(r);
}
}
rows_to_remove.sortReverse();
for (int r : rows_to_remove)
{
dataCopy.removeRow(r);
}
return dataCopy;
}
boolean isNumeric(String inputData) {
return inputData.matches("[-+]?\d+(\.\d+)?");
}
private static boolean isNullOrBlank(String s)
{
return (s==null || s.trim().equals(""));
}
这个问题可能更适合 Code Review Stack Exchange (note that if you post there, please link between crossposts and make sure you post a true MCVE 并明确这是一个处理问题,而不是 Java 问题),但我会尝试提供一些意见。
您可以通过添加好的行而不是删除坏的行来简化您的代码。通过仅复制 inputTable
中的列来创建 returnTable
,然后遍历 inputTable
并仅添加有效行。
另外,看看这个 if
语句:
if ( ! isNullOrBlank(value_string) ) {
if ( ! isNumeric(value_string) ) {
rows_to_remove.append(r);
}
}
else {
rows_to_remove.append(r);
}
这将在一种情况下保留一行:如果值不是 null
或空白并且它是数字。您可以使用单个 if
语句重写此逻辑:
if (!isNullOrBlank(rowValue) && isNumeric(rowValue)){
把它们放在一起,看起来像这样:
Table removeEmptyRows(Table inputTable, String columnName){
Table returnTable = cloneTable(inputTable);
returnTable.clearRows();
for (int row = 0; row < inputTable.getRowCount(); row++){
String rowValue = inputTable.getString(row, columnName);
if (!isNullOrBlank(rowValue) && isNumeric(rowValue)){
returnTable.addRow(inputTable.getRow(row));
}
}
return returnTable;
}
但请注意,此代码不一定比您的代码更好。它并没有更快。如果您理解您的代码,那么这就是最重要的事情。如果成功了,就不用太担心了"more elegant"。继续下一件事。
目标是 return 一个干净的 table,它只包含指定列中包含数字数据的行。 下面的代码对我有用。但我忍不住觉得有更好的方法来做到这一点。关于更优雅的解决方案的任何想法?
我有代码:
Table removeEmptyRows(Table data, String column_name)
{
IntList rows_to_remove = new IntList();
Table dataCopy = cloneTable(data);
for (int r = 0; r<dataCopy.getRowCount(); r++)
{
String value_string = dataCopy.getString(r, column_name);
///filter out the NaNs
if ( ! isNullOrBlank(value_string))
{
if ( ! isNumeric(value_string) )
{
rows_to_remove.append(r);
}
} else {
rows_to_remove.append(r);
}
}
rows_to_remove.sortReverse();
for (int r : rows_to_remove)
{
dataCopy.removeRow(r);
}
return dataCopy;
}
boolean isNumeric(String inputData) {
return inputData.matches("[-+]?\d+(\.\d+)?");
}
private static boolean isNullOrBlank(String s)
{
return (s==null || s.trim().equals(""));
}
这个问题可能更适合 Code Review Stack Exchange (note that if you post there, please link between crossposts and make sure you post a true MCVE 并明确这是一个处理问题,而不是 Java 问题),但我会尝试提供一些意见。
您可以通过添加好的行而不是删除坏的行来简化您的代码。通过仅复制 inputTable
中的列来创建 returnTable
,然后遍历 inputTable
并仅添加有效行。
另外,看看这个 if
语句:
if ( ! isNullOrBlank(value_string) ) {
if ( ! isNumeric(value_string) ) {
rows_to_remove.append(r);
}
}
else {
rows_to_remove.append(r);
}
这将在一种情况下保留一行:如果值不是 null
或空白并且它是数字。您可以使用单个 if
语句重写此逻辑:
if (!isNullOrBlank(rowValue) && isNumeric(rowValue)){
把它们放在一起,看起来像这样:
Table removeEmptyRows(Table inputTable, String columnName){
Table returnTable = cloneTable(inputTable);
returnTable.clearRows();
for (int row = 0; row < inputTable.getRowCount(); row++){
String rowValue = inputTable.getString(row, columnName);
if (!isNullOrBlank(rowValue) && isNumeric(rowValue)){
returnTable.addRow(inputTable.getRow(row));
}
}
return returnTable;
}
但请注意,此代码不一定比您的代码更好。它并没有更快。如果您理解您的代码,那么这就是最重要的事情。如果成功了,就不用太担心了"more elegant"。继续下一件事。