![]() |
|
|
|
| ||||||
|
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: arraylist, compare, help, how to, java, programming, tutorial |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| ||||
| 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); Hope that has helped, i have not tested but i am sure you can ![]() |
| |||
| I haven't used ArrayList at all, but I have used the compareTo() method quite a lot. compareTo() returns one of three possible values:
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));
} 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));
} rpgfan3233 Last edited by rpgfan3233 : 07-16-2007 at 04:10 PM. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-16-2007) | ||
| ||||
| Quote:
|
| ||||
| 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));
} 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) {
.....
} ![]() Thanx lee for the fast response ![]() |
| |||
| 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) {
.....
} 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. |
| The Following User Says Thank You to rpgfan3233 For This Useful Post: | ||
HelloWorld (07-16-2007) | ||
![]() |
| Thread Tools | |
| Display Modes | |
| |