3

My list of objects can have such elements eg1:

[vale11, value12, value13, null, null, null, value21, value22, value23, value31, value32, value33]

eg2:

[vale11, value12, value13, null, null, null, null, null, null, value31, value32, value33]

eg3:

[vale11, value12, value13, null, null, null, value21, value22, value23, null, null, null]

eg4:

[vale11, null, value13, null, null, null, value21, value22, value23, value31, value32, null]

I want to remove the null values but not all (note eg4) and only those in a range started from certain index. So in eg1 would be something like:

list.sublist(3, 6).clear();

eg2:

list.sublist(3, 6).clear();
list.sublist(6, 9).clear();//it's not going to work

I know the starting indexes and a number of next elements (always the same)

Sometimes it would be 1 range, sometimes 3 , 5 ... How to clear the original list with the use of a loop or streams ?


Paste Row Values not Formulas

Hi I've been searching around google for a while and not able to find the solution to my specific issue at the moment. So the problem is simple I want to select and copy a and paste only the values not the formulas generating the values.

I've checked : Copy Entire Row (Values not formulas) VBA

https://msdn.microsoft.com/en-us/library/aa195818(v=office.11).aspx

the current code I'm using is below I'm currently just using "OutputWorksheet.Paste" which is pasting the row but it also carries the formulas and tried "OutputWorksheet.PasteSpecial.xlPasteValues" but it keeps throwing "Compile Error: Expected Function or Variable"

Any help would be greatly appreciated or a better way of finding a Row based off the cboMADropDown value in one sheet copying the row and pasting to another sheet with just values no formulas.

With InputWorksheet                                                     ' Set sheet we want to search
Set FindRow = .Range("O:O").Find(What:=cboMADropDown, LookIn:=xlValues) ' Set FindRow container by searching Column F with cboMADropDown Value and look for the cell value to compare
End With                                                                ' End the With Statement
FindRowNumber = FindRow.Row                                             ' Set the FindRow row value to a number


InputWorksheet.Rows(FindRowNumber).EntireRow.Copy                       'Grab contents of entire row which is going to be updated
LastRow = OutputWorksheet.Range("A" & Rows.Count).End(xlUp).Row         'Finds the last blank Row on the MA tracking history sheet

OutputWorksheet.Activate                                                'Activate the worksheet so the .select command works with no errors
OutputWorksheet.Rows(LastRow).Select                                    'Select last blank row from MA tracking history
OutputWorksheet.Paste                        'Paste contents of the clipboard to backup
OutputWorksheet.Range("O" & LastRow).Value = TimeStamp                  'Add time stamp for the update
4

3 回答 3

2

您可以稍微改变一下您的技术:按降序排列初始索引上的范围。这里的基本原理是,只要您从较高的索引转到较低的索引,并且范围不“交叉”,索引就会保持一致。

因此,清除sublist(6, 9)之后sublist(3, 6)将毫无问题地工作:

//  0        1       2       3     4     5     6     7     8      9       10       11
[vale11, value12, value13, null, null, null, null, null, null, value31, value32, value33]
//                                           ^^^^^^^^^^^^^^^^ [6, 9)
//                         ^^^^^^^^^^^^^^^^ [3, 6)
于 2018-05-24T11:17:49.950 回答
0

使用可以通过循环来做到这一点:

下面的方法将删除其元素startIndex <= index <= endIndex

<T> List<T> chopList(List<T> originalList, int startIndex, int endIndex) {
    // Your code to check original list, validate startIndex, endIndex must inrange...
    return IntStream.range(0, originalList.size() - 1)
                    .filter(i -> i < startIndex || i > endIndex)
                    .mapToObj(originalList::get)
                    .collect(toList());
}
于 2018-05-24T11:18:39.687 回答
0

如果你想null从 a 中删除元素List,你可以这样做很简单

list.removeIf(Objects::isNull);

当然,您可以将操作限制在一定范围内,例如

list.subList(start, end).removeIf(Objects::isNull);

如果实际任务是从列表中删除预定义范围(并且这些位置可能存在null元素的信息实际上是无关紧要的),则可以使用此答案(按降序处理范围),如果范围不重叠.

如果范围可能重叠,您可以使用

BitSet toRemove = new BitSet(list.size());
toRemove.set(firstRangeStart, firstRangeEnd);
toRemove.set(secondRangeStart, secondRangeEnd);
// etc
for(int e = toRemove.length(), s; e > 0; e = toRemove.previousSetBit(s)+1)
    list.subList((s=toRemove.previousClearBit(e-1))+1, e).clear();

这将在按降序处理结果范围之前融合相邻和重叠的范围。

于 2018-05-24T17:38:14.733 回答