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:
• Campbell Ritchie
• Liutauras Vilda
• Jeanne Boyarsky
• Devaka Cooray
• Paul Clapham
Sheriffs:
• Tim Cooke
• Knute Snortum
• Bear Bibeault
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Ganesh Patekar
Bartenders:
• Frits Walraven
• Carey Brown
• Tim Holloway

# array comparison

Greenhorn
Posts: 29
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
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
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

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
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
Posts: 16084
88

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
when i = 0 => data[i-1] = data[-1]

Manuel Leiria
Ranch Hand
Posts: 171

naga raaju
Greenhorn
Posts: 29