Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Java Arrays

Java Arrays

Table of Contents

  • Java Arrays
  • Features of Java Arrays
    • Single Dimensional Array
    • Initialization of Array
    • Java Arrays Example
    • Multi-Dimensional Array
    • Jagged Array

Java Arrays

Java Arrays

An array in Java is a grouping of related data types. An object called an array serves as a container for values of the same type. Because an array’s size must be declared at the time of declaration, it is also referred to as a static data structure.

Array starts from zero index and goes to n-1 where n is length of the array.

So In Java, array is treated as an object and stores into heap memory. It allows to store pri-mitive values or reference values.

Array can be single dimensional or multidimensional in Java.


Features of Java Arrays

  • It is always indexed. Index begins from 0.
  • It is a collection of similar data types.
  • It occupies a contiguous memory location.
  • It allows to access elements randomly.

Single Dimensional Array

A single index is used to store elements in a single-dimensional array. Simply increasing an array’s index by one will return all of its elements.

Array Declaration

Syntax :

datatype[] arrayName;;
or
datatype arrayName[];

Java allows to declare array by using both declaration syntax, both are valid.

So The arrayName can be any valid array name and datatype can be any like: int, float, byte etc.

Example :

int[ ] arr;
char[ ] arr;
short[ ] arr;
long[ ] arr;
int[ ][ ] arr;   // two dimensional array.

Initialization of Array

Memory allocation for an array takes place during initialization. We specify the array’s size at initialization in order to set aside memory space.

Initialization Syntax

arrayName = new datatype[size]

new operator is used to initialize an array.

So The arrayName is the name of array, new is a keyword used to allocate memory and size is length of array.

We can combine both declaration and initialization in a single statement.

Datatype[] arrayName = new datatype[size]

Example : Create An Array

Lets create a single dimensional array.


class Demo
{
public static void main(String[] args)
  {
    int[] arr = new int[5];
        for(int x : arr)
        {
                System.out.println(x);
        }
   }
}
	

0 0 0 0 0

In the example above, we created an array called arr, which can hold 5 elements and is of the int type. The array prints five times zero to the console when we iterate through it to access its elements. Because we didn’t assign values to the array, all of its elements were initially initialised to 0, which is why it prints 0.

Set Array Elements

We can set array elements either at the time of initialization or by assigning direct to its index.

int[] arr = {10,20,30,40,50}; 

Here, we are assigning values at the time of array creation. It is useful when we want to store static data into the array.

or

arr[1] = 105

Here, we are assigning a value to array’s 1 index. It is useful when we want to store dynamic data into the array.

Java Arrays Example

Here, we are assigning values to array by using both the way discussed above.


class Demo
{
public static void main(String[] args)
  {
	int[] arr = {10,20,30,40,50}; 
        for(int x : arr)
        {
                System.out.println(x);
        }
        
     // assigning a value
        
        arr[1] = 105;
        System.out.println("element at first index: " 
+arr[1]); } }

10 20 30 40 50 element at first index: 105


Accessing array element

So By using an array element’s index value, we can access it. Using a loop or a direct index value, as appropriate. To iterate through the elements of an array, we can use a loop like for, for-each, or while.

Example to access elements


class Demo
{
public static void main(String[] args)
  {
	int[] arr = {10,20,30,40,50}; 
        for(int i=0;i<arr.length;i++)
        {
                System.out.println(arr[i]);
        }
       
        System.out.println("element at first index: "
+arr[1]); } }

10 20 30 40 50 element at first index: 20

So Here, we are traversing array elements using loop and accessed an element randomly.

Note:-To find the length of an array, we can use length property of array object like: array_name.length.


Multi-Dimensional Array

A single dimensional array and a multi-dimensional array are very similar. In contrast to a single-dimensional array, which can only have one row index, it can have multiple rows and multiple columns. Data is represented in a tabular format with rows and columns for storage.

Multi-Dimensional Array Declaration

datatype[ ][ ] arrayName;

Initialization of Array

datatype[ ][ ] arrayName = new int[no_of_rows][no_of_
columns];

The arrayName is the name of array, new is a keyword used to allocate memory and no_of_rows and no_of_columns both are used to set size of rows and columns elements.

So Like single dimensional array, we can statically initialize multi-dimensional array as well.

int[ ][ ] arr = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}
};

Java Arrays Example:


class Demo
{
public static void main(String[] args)
  {
	int arr[ ][ ] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,
13,14,15}}; for(int i=0;i<3;i++) { for (int j = 0; j < 5; j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } // assigning a value System.out.println("element at first row and
second column: " +arr[0][1]); } }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 element at first row and second column: 2


Jagged Array

Jagged array is an array that has different numbers of columns elements. In java, a jagged array means to have a multi-dimensional array with uneven size of columns in it.

Initialization of Jagged Array

Jagged array initialization is different little different. We have to set columns size for each row independently.


int[ ][ ] arr = new int[3][ ]; 
arr[0] = new int[3];
arr[1] = new int[4];
arr[2] = new int[5];
	

Example : Jagged Array


	class Demo
{
public static void main(String[] args)
  {
	int arr[ ][ ] = {{1,2,3},{4,5},{6,7,8,9}};
        for(int i=0;i<3;i++)
        {
        	for (int j = 0; j < arr[i].length; j++) {
        		
        		System.out.print(arr[i][j]+" ");
			}
        	System.out.println();
                
        }
        
   }
}

1 2 3 4 5 6 7 8 9

So Here, we can see number of rows are 3 and columns are different for each row. So This type of array is called jagged array.

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.