aspose file tools*
The moose likes Beginning Java and the fly likes array comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array comparison" Watch "array comparison" New topic
Author

array comparison

naga raaju
Greenhorn

Joined: Mar 14, 2008
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
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
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


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
naga raaju
Greenhorn

Joined: Mar 14, 2008
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

Joined: Jul 13, 2007
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

Joined: Mar 14, 2008
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
when i = 0 => data[i-1] = data[-1]
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
naga raaju
Greenhorn

Joined: Mar 14, 2008
Posts: 29
thanks for your help

bye
Naga
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
glad to help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array comparison