Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array comparison

 
naga raaju
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
 
Manuel Leiria
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
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