Introduction to Array || Java + DSA course || Blog 5

our phone starts to hang when we store too much . It means that our documents , videos , photos are taking too much space of the memory . The same way our code starts to hang when many variables are used differently as they are stored independently in the memory.

For a quick overview we will be learning;

  • Array
  • Ways to use array
  • 2D array
  • Take an array input/2D array input

An array is a collection of similar data elements stored at contiguous memory locations. It saves one type of arguments like int, float, string, etc.Instead of making many variables to save data, we save it together . This is called array.

It comes under user defined type. It store multiple items of the same type together. 

If the size is considered as 3,then three arguments can be saved but the index starts from 0. it will go as array[0], array[1], array[2].

public class Main{
public static void main(String args[]){
int[] arrayf = new int[3];//naming array as arrayf.int is type of array.
//3 is the size of aaray means 3 arguents can be saved
arrayf[0] = 97;//on index 0, argument will be 97
arrayf[1] = 95;//on index 1, argument will be 95
arrayf[2] = 93;//on index 2, argument will be 93
//so total three arguments are given. if 4 were given , it will not work
//the index starts from 0
for (int i=0 ;i < 3 ; i++){//using i as an index
System.out.println(arrayf[i]);
}
}
}


If you already know the argument and want to know make an array for after use, you can make it like:

int marks[] = {97,95,93,23,23};

 

If you want to know the number of arguments present in the array just write

System.out.println(marks.length);

Output will be 5.

The index of the last number will be marks.length - 1 since indexing starts from 0 while length from1.

To take input array:

import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int[] inputarray = new int[3];//only 3 variables
for(int i=0;i<3;i++){//since index starts from 0,i will go till 2.
                            counting (0,1,2)total 3 arguments
inputarray[i] = sc.nextInt();
}
for(int j=0;j<3;j++){//printing them in single line
System.out.print(inputarray[j] + " ");
}
sc.close();
}
}


2d Array

We have learnt matrix in our studying years. A matrix is a rectangular arrangement of numbers into rows and columns. 


 

Size: 3 * 5=15
Rows 3
Columns 5
Int size= 4 bytes. 
Total size= 4*15 = 60 bytes
 
To make a 2d array, simply:
 
int[][] array = new int[3][5];
 

 Here 3 is the row and 2 is the column

 to save an int in

 

inputarray[1][2] = 100;


The output:


Here 100 is saved in array(1st row, 2nd column).

Lets take an 2d arrray as input.

import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int rows = sc.nextInt();//row input
int col = sc.nextInt();//column input
int[][] twodarray = new int[rows][col];//making 2d array
for(int i = 0;i<rows;i++){
for(int j=0;j<col;j++){
twodarray[i][j] = sc.nextInt();//taking input
}
}
for(int i = 0;i<rows;i++){
for(int j=0;j<col;j++){
System.out.print(twodarray[i][j]+ " ");//printing array
}
System.out.println();
}
}
}

 

Input:

2

3

12 13 14

15 16 17


Output:

12 13 14

15 16 17


___________________________________________________________________________________


VISIT: https://javaanddsa.blogspot.com/

DID YOU LIKE OUR BLOG?

PLEASE TELL US IN THE COMMENT SECTION 

Comments

Post a Comment

Popular posts from this blog

LinkedList Problems for Beginners DSA || Java and DSA course || Blog 17

Introduction to Java || Java + DSA course || Blog 1

LinkedList in DSA || Java and DSA course || Blog 16