Java Array

Java Array:

We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it.

An array is a contiguous memory allocation of same-sized or homogeneous data-type elements. The word contiguous means the array elements are located one after the other in memory. The term same-sized means that each array element occupies the same amount of memory space. The size of each array element is determined by the type of objects an array is declared to contain. So, for example, if an array is declared to contain integer primitive types, each element would be the size of an integer and occupy 4 bytes. If, however, an array is declared to contain double primitive types, the size of each element would be 8 bytes. The term homogeneous is often used in place of the term same-sized to refer to objects having the same data type and therefore the same size.

Java Array Diagram

.Example 1:

Java Array Code 01
public class Array {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String[] kitchen = {"Chef's Knife","Mixing Bowls","Cutting Boards","Cups and Spoons"};
		
		System.out.println(kitchen[2]);
	}

}

Example 2: Replacing the third array value i.e. Cutting Boards by Spice Grinder.

Java Array Code 02
public class Array {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String[] kitchen = {"Chef's Knife","Mixing Bowls","Cutting Boards","Cups and Spoons"};
		
		kitchen[2] = "Spice Grinder";
		
		System.out.println(kitchen[2]);
	}

}

Example 3: Java Array using for Loop.

java array using for loop
public class Array {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String[] kitchen = new String [4];
		
		kitchen[0]= "Chef's Knife"; 
	    kitchen[1]= "Mixing Bowls";
	    kitchen[2]= "Cutting Boards";
	    kitchen[3]= "Cups and Spoons";
	    
	    for (int x=0; x<kitchen.length; x++) {
	    	
	    	System.out.println(kitchen[x]);
	    }
				
	}

}

Create MySQL Database and Connection in Python
Python Random Password Generator
Create a Text Editor in Python
Create JSON File in Python
Procedure-Oriented Programming
Object-Oriented Programming (OOP)
World Wide Web (WWW)
Java (programming language)– Wikipedia

Comments (No)

Leave a Reply