aspose file tools*
The moose likes Beginning Java and the fly likes Problem with Array Value - Comparing Arrays. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Array Value - Comparing Arrays." Watch "Problem with Array Value - Comparing Arrays." New topic
Author

Problem with Array Value - Comparing Arrays.

Dixon Alexander
Ranch Hand

Joined: Jan 16, 2004
Posts: 48
I have an array
array1[] that has 3 values.
example: dog
cat
chicken
String[] array1 = {"dog", "cat", "chicken"};

What I need to do use a if statement in this situation?
Example:
if (array1 == "dog" ) {
println("is a dog");
}
OR
if (array1.equals("dog")) {
println("is a dog");
}
Can any of these, too be possible, How, I try both cases show above but they don't work.
Thanks.


SJCP 1.4<br /> <br />"Go in there and do the best you can. That's all you can do."<br />Tiger Woods<br /> <br />"Practice is the best of all instructors."<br />Publilius Syrus (b. 42 AD)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

You're comparing the array object to a String; they're definitely not equal. You want to compare one element, or all the elements, to the String. To compare one particular element, you'd use
if (array1[1].equals("dog"))
To compare them all, you could use a for loop, or the following trick:
if (Arrays.asList(array1).contains("dog"))
using the "asList()" static method in the java.util.Arrays class.


[Jess in Action][AskingGoodQuestions]
Dixon Alexander
Ranch Hand

Joined: Jan 16, 2004
Posts: 48
import java.util.*;
public class TestingArrays {
public TestingArrays() {
}
public static void main(String[] args) {
TestingArrays tt = new TestingArrays();
String[] parentid = {"1", "1", "1", "dog", "cat"};
String str = new String("1");
// Fixed-size list
List list = Arrays.asList(parentid);
// Growable list
list = new LinkedList(Arrays.asList(parentid));
Vector v = new Vector();
for ( int i=0; i<parentid.length; i++ ) {
v.add(parentid[i]);
}
System.out.println("v : " + v);
Enumeration e = v.elements();
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
System.out.println("list.size(): " + list.size());
System.out.println("list.contains(str) " + list.contains(str));
System.out.println("");
System.out.println("array size: " + parentid.length);
for ( int i=1; i<= parentid.length; i++) {
if (Arrays.asList(parentid).contains("2")) {
System.out.println("la la la: " );
}
}
}
}
Dixon Alexander
Ranch Hand

Joined: Jan 16, 2004
Posts: 48
IN THE ABOVE SAMPLE PROGRAM
I like to prove that I have three "1" and then a "cat" and a "dog"
.....
I wanna be able to run a sql query where if the userID="1"
I like to perform a different behavior than if ther userID="something else".
"1" is a default value in the database table for users.
Thanks.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
if (Arrays.asList(array1).contains("dog"))

Thanks for the tip Ernest
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Array Value - Comparing Arrays.
 
Similar Threads
Array Assignment
Which is better?
varstatus in nested foreach
Array Reference assignment
How to do �shallow copy� and �deep copy�?