The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > Java


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , , , , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-16-2007, 02:45 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon9 How to compare value in ArrayList?

Anybody knows..?
I'm stuck on getting the element of a certain index in ArrayList and compare them with another element in other index..

I tried the following method in ArrayList, but have no clue how to compare them since it's an Object. I was researching on it and it seems that we need to use compareTo() method, but I personally never use it XD

This is what I'm planning to do:
[code]
ArrayList al = new ArrayList();
if (al.get(index_1) < al.get(index_2) {
......
}
[code]

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 07-16-2007, 02:53 PM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 616
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
As you know i am not a java guru but i know how much of a pain this is, i have looked up a little and from what i have seen you need something like:

Code:
//Setup booleans
    boolean[] bArr1 = null;
    boolean[] bArr2 = null;
    boolean b = Arrays.equals(bArr1, bArr2);

//To compare, your boolean b is = to the 2 arrays...
    b = Arrays.equals(array with value 1, array  with value 2);
If they match then the result of b will be true, otherwise it will be false, you can then you b to decide weather to do one thing or the other.

Hope that has helped, i have not tested but i am sure you can
Reply With Quote
  #3 (permalink)  
Old 07-16-2007, 04:02 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
I haven't used ArrayList at all, but I have used the compareTo() method quite a lot. compareTo() returns one of three possible values:
  • -1: value1 < value2
  • 0: value1 = value2
  • 1: value1 > value2
However, you shouldn't need it unless you have a specific class that requires compareTo(), such as String. With char, int, etc., you only need the normal logical comparison operators. Also, after doing my own bit of research, ArrayList should actually be used like this:
Code:
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(4);
al.add(9);
/* Remember that each element is simply a java.lang.Integer, the class
     encapsulating the primitive data type 'int'. */
if (al.get(0) < al.get(1)) {
    System.out.println("Minimum value: " + al.get(0));
    System.out.println("Maximum value: " + al.get(1));
}
else { //al.get(0) >= al.get(1)
    System.out.println("Minimum value: " + al.get(1));
    System.out.println("Maximum value: " + al.get(0));
}
No compareTo is necessary. However, if you really want to use it, it would be like the following:
Code:
if (al.get(0).compareTo(al.get(1)) == -1) { //if (al.get(0) < al.get(1))
    System.out.println("Minimum value: " + al.get(0));
    System.out.println("Maximum value: " + al.get(1));
}
else { //al.get(0) >= al.get(1), or al.get(0).compareTo(al.get(1)) > -1
    System.out.println("Minimum value: " + al.get(1));
    System.out.println("Maximum value: " + al.get(0));
}
I hope this helps!

rpgfan3233

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by rpgfan3233 : 07-16-2007 at 04:10 PM.
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-16-2007)
  #4 (permalink)  
Old 07-16-2007, 06:20 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Originally Posted by Lee View Post
As you know i am not a java guru but i know how much of a pain this is, i have looked up a little and from what i have seen you need something like:

Code:
//Setup booleans
    boolean[] bArr1 = null;
    boolean[] bArr2 = null;
    boolean b = Arrays.equals(bArr1, bArr2);
 
//To compare, your boolean b is = to the 2 arrays...
    b = Arrays.equals(array with value 1, array  with value 2);
If they match then the result of b will be true, otherwise it will be false, you can then you b to decide weather to do one thing or the other.

Hope that has helped, i have not tested but i am sure you can
Haha, I knew this one already in particular, but in this case, I would compare whether it's equal or not by using .equals on my object. I gave an example that I want to compare whether it's less than some other object that I got from the ArrayList

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 07-16-2007, 06:24 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Code:
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(4);
al.add(9);
/* Remember that each element is simply a java.lang.Integer, the class
     encapsulating the primitive data type 'int'. */
if (al.get(0) < al.get(1)) {
    System.out.println("Minimum value: " + al.get(0));
    System.out.println("Maximum value: " + al.get(1));
}
else { //al.get(0) >= al.get(1)
    System.out.println("Minimum value: " + al.get(1));
    System.out.println("Maximum value: " + al.get(0));
}
For some reason this method doesn't work on my code, but that's probably I should try it out by using ArrayList<Integer> al = new ArrayList(); because currently, I'm only using ArrayList al = new ArrayList();

I've seen that method quite often too, but I'm not sure how does it work if I want to create a method that will take ArrayList<Integer> I tried to do this, but it gave me an error:

Code:
public void meth(ArrayList<Integer> al) {
        .....
}
But I think I need to double check again... thanx though rpgfan3233, helps a lot
Thanx lee for the fast response

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #6 (permalink)  
Old 07-16-2007, 07:05 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
I'm guessing you are calling meth() from a static method such as main(). If you are calling the meth() method from the main() method, for example, you would need to add the static keyword:
Code:
public static void meth(ArrayList<Integer> al) {
    .....
}
Otherwise, you would get an error like this:
ArrayListTest.java:24: non-static method meth(java.util.ArrayList<java.lang.Inte
ger>) cannot be referenced from a static context
meth(al);
^
1 error

I had called meth(al) from main(). Once I added the static keyword, it was fine.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-16-2007)
  #7 (permalink)  
Old 07-16-2007, 11:23 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Haha, thanx a lot, I just found out the problem that I haven't set Eclipse's Compiler to be 6.0 (isn't that horrible... XD) lol....

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 01:00 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50