• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

array comparison  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have an array like this
double[]={34,32,45,65,76,54,77,55,767,76,4,566,66};

i want to comapre the array elements with previous elemnts.

like 66 with 566 and 566 with 4 like this

i need code in java.

please send the code or guide me

thanks and regards
naga
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and if you wish I can spoon feed you!!!

Show us what have you done so far and where are you stuck. We don't do homeworks
 
naga raaju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
double [] data={34,32,45,65,76,54,77,55,767,76,4,566,66};

for(int i=data.length-1;i>=0;i--)
{
if(data[i]>data[i+1])
{
System.out.println("::"+data[i]);

}

}
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by naga raaju:
double [] data={34,32,45,65,76,54,77,55,767,76,4,566,66};

for(int i=data.length-1;i>=0;i--)
{
if(data[i]>data[i+1])
{
System.out.println("::"+data[i]);

}

}


and do you think this works?

Try to think first!
 
naga raaju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
double [] data={34,32,45,65,76,54,77,55,767,76,4,566,66};

for(int i=data.length-1;i>=0;i--)
{
if(data[i]>data[i-1])
{
System.out.println("::"+data[i]);

}

}
why i am getting array index out of bound exception
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by naga raaju:
why i am getting array index out of bound exception


You're accessing data[i-1] in your code. What happens there if i == 0?
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i = 0 => data[i-1] = data[-1]
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
naga raaju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your help

bye
Naga
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
glad to help!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!