How to read a specific item in a vector? - vector

I have a viewScope "selection" which is a vector.
I would like to be able to read a specific elementh (third in this case)
I thought dooing it with an iterator like this , but it just gives me all the elements instead of the third...
var i = 0;
for (var it = viewScope.selection.iterator();it.hasNext();i++ ) {
if (i == 3){
sessionScope.example="item "+i+"="+it.next();
}
}

Maybe I don't really understand your question, but you can get the third element via index, i.e. like
viewScope.selection[2]
or
viewScope.selection.get(2)

Related

Laravel collection condition in sum

I have a collection of transactions with relations and I would like to sum column separated by condition of relation column. Right now I have this:
$delegatedProvision = 0;
$ownProvision = 0;
foreach ($transactions as $transaction) {
if ($transaction->discount->consider_improvement) {
$delegatedProvision += $transaction->stats->$column;
continue;
}
$ownProvision += $transaction->stats->$column;
}
$this->salesCollection->put('delegatedProvision', $delegatedProvision);
$this->salesCollection->put('ownProvision', $ownProvision);
It works but I would like to use Laravel collections. So far I have just this:
$provision = $transactions->sum(function ($transaction) use ($column) {
return $transaction->stats->$column;
});
And I don't know how to use condition in sum() method and according column $transaction->discount->consider_improvement (which is boolean) have sum in separated variables. I can use filter each for different consider_improvement but it means that I have to iterate all transactions twice.
Try this:
$collection->where(/* your condition */)->sum($column);

How to add new item in specific index?

I new in kotlin , i want to update an item in lists.
I use this code:
var index: Int
for (record in recordList)
if (record.id == updatedHeader?.id) {
index = recordList.indexOf(record)
recordList.add(index, updatedHeader)
}
but it cant do this, because of ConcurrentModificationException
Assuming that recordList is a MutableList and val (so, you'd like to modify the records in place), you can use forEachIndexed to find the records you care about and replace them.
This did not cause a ConcurrentModificationException:
recordList.forEachIndexed { index, record ->
if(record.id == updatedHeader?.id) recordList[index] = updatedHeader
}
On the other hand, if you redefine recordList as a non-mutable list, and a var, you could rewrite the entire list using map:
recordList = recordList.map { if(it.id == updatedHeader?.id) updatedHeader else it }
Of course, you could call .toMutableList() on the end of that if you wanted to turn your List into a MutableList.
If there's a single record with the given id in the list, you can find its index and add the header at that index:
val index = recordList.indexOfFirst { it.id == updatedHeader.id }
if (index >= 0)
recordList.add(index, updatedHeader)
If there are multiple records with the given id and you want to prepend header before each of them, you can use get listIterator and use its methods to modify the list during the iteration without getting ConcurrentModificationException:
val iterator = recordList.listIterator()
for (record in iterator) {
if (record.id == updatedHeader.id) {
iterator.previous() // move to the position before the record
iterator.add(updatedHeader) // prepend header
iterator.next() // move next, back to the record
}
}

How do I make UpdateOrInsert in Kotlin collections

I have a list of items, and one of item copies is changed by user, how do I find it in my collection by Id and update, or if it's not found I'd like to add the item? my best guess is, but it requires ugly indexOf(v)
fun updateOrInsert(note : UserNote) {
val list = notes.value!!
val v = list.firstOrNull{(Id) -> Id ==note.Id}
if (v==null) {
list.add(note)
} else {
val i = list.indexOf(v)
list[i] = note
}
notes.value = list
}
Use indexOfFirst to find the index of the first element with the given ID. If -1, add the item to the list, otherwise, change the value at the found index.

Using Count To Split A Checklist Into 2 Columns in X++ Fetch Method

Here is what I have so far, this is returning two columns, but each counter is stopping and then duplicating the same value over and over...
if(lLogisticsControlTable.APMJobTypeId)
select count (RecID) from jobTypeCheck where jobTypeCheck.APMJobTypeId == lLogisticsControlTable.APMJobTypeId;
{
counter = jobTypeCheck.RecId;
}
while select jobTypeCheck where jobTypeCheck.APMJobTypeId == lLogisticsControlTable.APMJobTypeId
{
counter1 = counter / 2;
halfCount1 = counter - counter1;
if(halfcount <= counter1)
{
halfCount++;
jobListCheck1 = jobTypeCheck.Name;
}
if (halfCount1 > halfCount)
{
halfCount1++;
jobListCheck2 = jobTypeCheck.Name;
}
element.execute(2);
}
}
As Michael Brown indicated, it's difficult to understand the problem with half of the code ;)
However, I would suggest that you call the element.execute(2) method on every second pass through the loop? That way jobListCheck1 would be on the left, and jobListCheck2 would be on the right hand side. Finally you would then need to check immediately outside of your loop if you had an odd number of jobTypeCheck elements, and call the element.execute(2) method one last time remembering to set the jobListCheck2 variable as empty beforehand.
Regards

dynamically build Intersect statement ASP.NET

I would like to use the IEnumerable function Intersect() to combine a few list and get the similar integers from each list. The problem I'm faced with is that I don't know how many list I will need to compare.
Here is an example:
A{1,2,3,4}
B{1,2,3}
C{1,2}
results = A.Intersect(B).Intersect(C)
This works great, but the next time around I may have a D{1,2} next time I come across the function.
I'd like to use the Intersect method, but I'm open to new ideas as well.
If you are receivng the collections in a list, you could do this:
List<List<int>> lists = new List<List<int>>();
var result = lists[0].AsEnumerable();
for (int i = 0; i < lists.Count - 1; i++)
{
result = result.Intersect(lists[i + 1]);
}

Resources