• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hi,



I am having the Arraylist it is having the values(10,11,12,14,15,16) it's series of element but 13 is missing if that arraylist is not in series i need to quit. this is working fine but the first index of the arraylist is not coming. i.e 10 always it's coming 11. in my logic please correct me that logic i should have the output [10,11,12].


please help me to solve this problem
......................
public class Test {

public static void main(String[] args) {

ArrayList<Integer> arr = new ArrayList();
arr.add(10);
arr.add(11);
arr.add(12);
arr.add(14);
arr.add(15);
arr.add(16);

int lastvalue = 0;
int currentvalue;
ArrayList<Integer> container = new ArrayList();
for (int rr : arr) {
currentvalue = rr;
if (lastvalue != 0) {
if (currentvalue - lastvalue == 1) {
container.add(rr);
// System.out.println(container.toString());
}else {
System.out.println(container.toString());
}
}
lastvalue = currentvalue;

}
}
}


thanks and regards
Venu J


 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just from a quick look, you appear to want to add lastValue to the container when you set lastValue to currentValue. container never contains the very first value (ie: 10) because you never add it.
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic