-4

What is the best way to check the existence of a certain array name? for example, if i had a array called:

String array1[] = new String[]{"abc", "def", "ghi"};

what is the best way to check if array1 exists or not?

Additionally, is there a way to see if the string "abc" exists inside the array if we don't know which index "abc" is in?

EDIT: I'm planning to have the user input a string and check if the input string matches with any of the string array variable names

user3748353
  • 65
  • 1
  • 1
  • 5
  • 7
    If you declare `array1`, then it exists – Barranka Jun 29 '14 at 06:01
  • In which situation will you use this? – Christian Tapia Jun 29 '14 at 06:02
  • 1
    You probably meant: `String[] array1 =...` – Nir Alfasi Jun 29 '14 at 06:03
  • 2
    `if(Arrays.asList(array1).contains("abc"))` – BitNinja Jun 29 '14 at 06:04
  • You really should learn about _objects_, _references_, and _variables_. I suggest you work through [The Java Tutorials](http://docs.oracle.com/javase/tutorial/), starting with the trails covering the basics. Once read, you realize that _objects don't have a name_. A variable is just a reference to an existing object. Furthermore: The existence of a variable and the excistence of an object are independent. – Seelenvirtuose Jun 29 '14 at 06:30

1 Answers1

0

To check whether the array exists you could use

if(array != null){
//some code for when the array exists
} else {
//some other code for when the array does not exist.
}

If you want to check, whether an array has more than 0 entries, please edit your question.

For checking if the string "abc" is part of the array, you can use

if(Arrays.asList(array1).contains("abc")){...}

To learn more about arrays, you should read the documentation for arrays:

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html