...

/

Array Examples: Part 4

Array Examples: Part 4

This lesson discusses more challenging examples of arrays to enhance your knowledge and understanding.

Coding example: 41

As we have said earlier, Java has many built-in array methods that can help us manipulate an array according to our requirements.

Press + to interact
import java.util.Arrays;
/*
We can check if any array has a certain value
*/
public class ExampleFortyOne {
public static void main(String[] args){
//array declaration
int[] myNumber = new int[3];
//now we need to add elements
myNumber[0] = 50;
myNumber[1] = 60;
myNumber[2] = 70;
//etc
int[] anotherNumber = {1, 2, 3};
String[] nameColection = {"John", "Bob", "Mary"};
System.out.println("Does array nameCollection contains this element? "
+ Arrays.asList(nameColection).contains(2));
System.out.println("Does array nameCollection contains this element? "
+ Arrays.asList(nameColection).contains("John"));
}
}

Code explanation

In the first case, we have passed a numerical value. As expected, the answer will be false. Because the array was of the String type, the next one is ...

Access this course and 1400+ top-rated courses and projects.