0

How do I use a value from the previous iteration in my current loop? I am generating a double [] v and want to do v - w where w is the value of v in the previous iteration. Obviously I need to implement subtraction for 2 arrays.

if (!e.getValueIsAdjusting()) 
                {
                    double[] v = new double[uaCount.get(table.getValueAt(table.getSelectedRow(),0)).singleValues.size()];
                    int i = 0;
                    for(Long j : uaCount.get(table.getValueAt(table.getSelectedRow(),0)).singleValues) 
                    {
                      v[i++] = j.doubleValue();
                    }
                    double [] w, r;
                    //implement r = v-w
                    System.out.println(String.valueOf(uaCount.get(table.getValueAt(table.getSelectedRow(),0)).singleValues));
                    dataset.addSeries("Histogram",r, 1000, 0, 120000);  
                }

the purpose of this is to add a new dataset to my histogram while getting rid of the previous one. Now that I think about this more though, subtracting the values will give me different answers right?

user2007843
  • 609
  • 1
  • 12
  • 30
  • Can you show us what you have tried so far? – Keppil Apr 24 '13 at 20:16
  • use lists, make your life easier – 0x6C38 Apr 24 '13 at 20:18
  • You've asked a [series of duplicate questions](http://stackoverflow.com/users/2007843/user2007843) on this topic going back to this [question](http://stackoverflow.com/q/16085734/230513). I've cited several complete examples. As suggested [here](http://stackoverflow.com/a/16176221/230513), you can "Replace the entire dataset in the plot." Please edit your question to include an [sscce](http://sscce.org/) that shows your current approach. Use a _small_ `TableModel` having a _few_ rows and columns. – trashgod Apr 24 '13 at 23:38

3 Answers3

3

How do I use a value from the previous iteration in my current loop?

Just save the loop's current value as previous as the last line in the end of the loop.
In your first line of the loop when you re-enter, previous has the previous value

Cratylus
  • 52,998
  • 69
  • 209
  • 339
1

declare a variable outside your loop to hold it

int val;
int prev;

for(int i=0; i<n; i++)
{
    //do stuff with val
    ...
    prev = val;
}
0

Assuming from your explanation your reuirment is to do something like:

for(initialization;condition;increment){
curr[i] = i;//holds your current ittration value
if(prev != null){
//do some operation
//these extra things can also be done by cure[i-1] instead of using extra variable just    //check if i>0 then get the previous value from your curr array and perform operation //accordingly
}
prev = curr[i];
}

Approach 2: As suggested by other users you can make use of collection framework which will be more simpler.

Jayaram
  • 1,715
  • 18
  • 30