Search⌘ K
AI Features

Array Examples: Part 4

Understand how to declare, initialize, and manipulate Java arrays through practical examples. Learn about arrays of objects, using methods to modify object states, and overcoming Java arrays' limitations with external libraries. This lesson helps you write more flexible and practical array code in Java.

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.

Java
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 ...