Posts

Showing posts with the label loops

Java Streams for looping twice over same list

Java Streams for looping twice over same list I am new to java 8, and i have a problem understanding streams for some reason. Let's say we have a list of objects List< MyObject >, where MyObject has 2 fields : Long Id, Date insertTime, and i would like to remove elements with same ID and earlier time. With 2 for loops it is something like this : for(MyObject object : myObjects) { for(MyObject tmpObject : myObjects) { if(object.getId() == tmpObject.getId()) { if(object.getInsertDate().after(tmpObject.getInsertDate())) myObjects.remove(tmpObject); else myObjects.remove(object); } } } How would this look when using streams? @Michael my bad. Than i would need another list or array to store the response data. Let's say i have So i need as a result Thank you all. "With 2 for loops it is something like this" I'm afraid it's not, because you can't remove from a list while y...

How to repeat input using a while loop with a sentinal

How to repeat input using a while loop with a sentinal I need help with regards to where to put a while loop on this code. I just began studying Java a few weeks ago. I would like to write in a sentinel value of -1, if entered, the program shall exit. As long as user input is not -1, keep asking repeating the program. When I put the while loop, "while(currentPop != -1)" , below the first question, "Enter current population", the program runs its first course successfully. However, it does not loop back to the first question. Instead, it goes directly to the second question, "Enter birth rate:". How shall I proceed and make sure that the first question keeps getting asked after running through the loops? Thank you all! import java.util.Scanner; public class Population { public static void main(String args) { Scanner scan = new Scanner(System.in); double birthRate, deathRate; double currentPop = 0; double newPop; ...

why can't we use async:true in loop?

why can't we use async:true in loop? I have gone through the question Javascript - AJAX request inside loops. I understood the question and answer. var totalQuotes = 1; var url = http://whisperingforest.org/js/getQuote(1).php; var amount =0; var sum = 0; var bookingAmount = 0; while (counter < 6) { $.ajax({ url:url, async: false, dataType: 'json', success:function(data) { jQuery.each(data, function(index, item) { amount = amount + item.amount; sum = sum + item.sum; bookingAmount = bookingAmount + item.bookingAmount; }); $('.amount').val(amount); $('.sum').val(sum); $('.bookingAmount').val(bookingAmount); } }); totalQuotes++; url = "http://whisperingforest.org/js/getQuote(" + totalQuotes + ").php"; counter++; } In above code why can't we use async:true ? What will...

How to make empty lists in MATLAB Live Script and append to them both outside of a loop and inside one? [closed]

How to make empty lists in MATLAB Live Script and append to them both outside of a loop and inside one? [closed] The three list I want to make are the following and I'm not sure how it's done in MATLAB. na = nb = t Outside of the list I want to append the follwoing: NA = input('Enter intial number of atoms of A > '); NB = input('Enter intial number of atoms of B > '); tau_a = input('Define decay constant of A >'); tau_b = input('Define decay constant of B >'); time = input('the total time >'); dt = input('Define time increment >'); I've to append the values like so: na = [na, NA] nb = [nb; NB] t = [t, 0] Finally, inside the loop I have tried appending values like this: for i = int16(time/dt) nai = na[i]-(na[i]/tau_a)*dt nbi = nb[i]+((na[i]/tau_a)-(nb[i]/tau_b))*dt ti = t[i]+dt na = [na, nai] nb = [nb, nbi] t = [t, ti] end But it is not working. It does not want to append the values. ...

subset a dataframe based on a matrix of row numbers and save the result in one list

subset a dataframe based on a matrix of row numbers and save the result in one list I have a data frame called df that looks like: > df Date A B C 1 2001 1 12 14 2 2002 2 13 15 3 2003 3 14 16 4 2004 4 15 17 5 2005 5 16 18 6 2006 6 17 19 7 2007 7 18 20 8 2008 8 19 21 9 2009 9 20 22 10 2010 10 21 23 and a matrix called index that looks like: > index Resample01 Resample02 Resample03 Resample04 Resample05 [1,] 1 7 1 2 7 [2,] 3 9 2 3 8 [3,] 5 1 3 8 1 [4,] 8 3 4 9 4 [5,] 10 4 5 10 9 The numbers in each column stands for the row number to be selected. The aim is to split the dataframe into two exclusive groups of "train" and "test" according to the row numbers in each column of the matrix "index". For exampl...