0% found this document useful (0 votes)
24 views

Array

ABOUT ARRAY ON JAVA

Uploaded by

deepz.bisht234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Array

ABOUT ARRAY ON JAVA

Uploaded by

deepz.bisht234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ARRAY IN JAVA

Imagine a Shoe Rack

An array in Java is like a shoe rack that can hold a specific number of shoes. Here's the
analogy:

● Shoe Rack (Array): This is the container that holds the shoes (elements).
● Number of Shoes (Size): The shoe rack is designed to hold a fixed number of shoes, just
like an array has a predetermined size when it's created.
● Each Shoe (Element): The individual shoes represent the elements stored in the array. They
can all be the same type (e.g., sneakers, sandals), just like array elements must be of the
same data type (e.g., all integers, all strings).
● Shoe Position (Index): Each shoe has its own designated spot on the rack, identified by its
position. Similarly, each element in an array has a unique index, starting from 0 and going up
to the size minus 1 (because counting starts from 0).

Real-Life Examples of Arrays

1. Shopping List: You can create an array to store your grocery list items:
Java
String[] shoppingList = {"bread", "milk", "eggs", "apples"};

Here, shoppingList is the array, String is the data type (all elements are text), and each item
like "bread" is an element accessed by its index (0 for "bread", 1 for "milk", and so on).
2. Movie Scores: You can use an array to keep track of your movie ratings:
Java
int[] movieScores = {4, 5, 3, 5};

In this case, movieScores is the array, int is the data type (all elements are integers), and
each score like 4 is an element with its corresponding index.

Declaring and Using Arrays

1. Declaration: You tell Java what kind of array you want (data type) and give it a name:
Java
int[] numbers; // Declares an array of integers named 'numbers'

2. Initialization (Creating the Shoe Rack): You allocate memory to hold the elements and
specify the size:
Java
numbers = new int[5]; // Creates an array 'numbers' that can hold 5
integers

3. Adding Elements (Putting Shoes on the Rack): You assign values to elements using
their index:
Java
numbers[0] = 10;
numbers[1] = 20;
// ... and so on for other elements

4. Accessing Elements (Picking Up a Shoe): You retrieve an element's value using its
index:
Java
int firstNumber = numbers[0]; // Assigns the value at index 0 (10)
to 'firstNumber'
Important Points to Remember:

● The size of an array is fixed once it's created.


● Array indexes start from 0, not 1.
● Trying to access an element outside the valid range (0 to size-1) will cause an error.

In Summary

Arrays are a fundamental data structure in Java that provide a structured way to store
collections of elements of the same type. By understanding the shoe rack analogy and the basic
operations, you can effectively use arrays in your Java programs!

You might also like