0% found this document useful (0 votes)
4 views10 pages

12-ARRAY (1)

HopeConflict Technology offers online and in-person classes for technical languages such as full stack development, alongside project development and consultancy services. The document provides an overview of arrays in programming, including their creation, advantages, disadvantages, and examples of one-dimensional and two-dimensional arrays. It also includes classwork and homework exercises, as well as interview questions related to arrays.

Uploaded by

Its-Omkar -Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

12-ARRAY (1)

HopeConflict Technology offers online and in-person classes for technical languages such as full stack development, alongside project development and consultancy services. The document provides an overview of arrays in programming, including their creation, advantages, disadvantages, and examples of one-dimensional and two-dimensional arrays. It also includes classwork and homework exercises, as well as interview questions related to arrays.

Uploaded by

Its-Omkar -Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

HOPECONFLICT TECHNOLOGY

ARRAY

HopeConflict is leading provider of both the online and on location


classes of technical languages like full stack development, also
provides services like project development, web development, and
Consultancy services.

support@hopeconflict.com
HOPECONFLICT TECHNOLOGY

1. Array is a collection of elements of homogeneous data element


elements.

2. It is a collection of similar data type.

3. At the time of array creation it is necessary to specify the size of array

otherwise we will get compile time error.

4. It is legal to have an array with size “0”.

5. If we take array size with negative value then we will get run time

exception that is negative value exception.

6. If we are trying to access array element with out of range index we will

get exception that is array index out of bound exception.

7. Length is a final variable that is applicable only for array. It represent a


size of array.

 Creating An Array :

1. Declaring the array.

2. Creating memory location for the array elements.

3. Putting the value of array elements in to the memory locations.

 Syntax :

Data_Type array_name[ ]=new Data_Type[size];

Example :

int arr[ ]=new int[5];

Char arr[ ]=new char[5];

String arr[]=new String[5];

Note : Array start with zero location and end with n-1.
support@hopeconflict.com
HOPECONFLICT TECHNOLOGY
 Graphical Representation :

int arr [ ] = new int [5]; //array creation with

size 5 length =size-1

Size=size-1

 int arr [ ] = new int [5]; //array creation with size 5


 arr[0]=5;
 arr[1]=55;
 arr[2]=6; // assign value to index position.
 arr[3]=9;
 arr[4]=3;
 Output :
 5 55 6 9 3

support@hopeconflict.com
HOPECONFLICT TECHNOLOGY

 Example :
package arr;

import java.util.Scanner;

public class afrraycreation {

public static void main(String[] args)


{
// data_type array_name[]=new data_type[size];

Scanner sc=new Scanner(System.in);


int sum=0;
float avg;

System.out.println("enter the size of array :");


int size=sc.nextInt();

int arr[]=new int[size];

System.out.println(" enter array

elements");

for(int i=0;i<arr.length;i++)
{
arr[i]=sc.nextInt();
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}

support@hopeconflict.com
HOPECONFLICT TECHNOLOGY
TWO DIMENSIONAL ARRAY
 Syntax :

int arr [ ] [ ] = new int [5][5]; //array creation of 5 by 5 matrix

 graphical representation :

00 01 02

10 11 12

20 21 21

 int arr [ ][ ] = new int [3][3];


 arr[0][0]=5;
 arr[0][1]=5;
 arr[0][2]=5;
 arr[1][0]=6;
 arr[1][1]=6;
 arr[1][2]=6;
 arr[2][0]=7;
 arr[2][1]=7;
 arr[2][2]=7; // assign value to index position.
Output :
 5 5 5
 6 6 6
 7 7 7

support@hopeconflict.com
HOPECONFLICT TECHNOLOGY
 Example :

package ClassWork;
importjava.util.Scanner;

public class MatrixDemo


{
void matrix()
{
int row_size,col_size;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the size of row:");


row_size=sc.nextInt();

System.out.println("Enter the size of column:");


col_size=sc.nextInt();

int matrix1[][] = new int[row_size][col_size];

System.out.println("enter array elements::");

for(int row=0;row<row_size;row++)
{
for(int col=0;col<col_size;col++)
{
matrix1[row][col]=sc.nextInt();
System.out.print(" "+matrix1[row][col]+" ");
}
System.out.println(" ");

}
public static void main(String[] args)
{
MatrixDemo m=new MatrixDemo();
m.matrix();

support@hopeconflict.com
HOPECONFLICT TECHNOLOGY
 Advantages :

1. We can fetch the data from particular index.


2. Easy to define.
3. Easy to understand.

 Disadvantages :

1. Fix in size.
2. Can hold only homogeneous data element only.
3. No in build method support is available for every requirement

 Class Work :
1. Write a Java program to calculate the average value of array elements

2. WAP sort array elements in ascending order.

3. WAP to create transpose of a matrix (transpose is converting rows to


columns) and print it.

4. Number of pairs in an array. Give an array of N elements, that ask is to


find all the unique pairs that can be formed using the elements of a
given array.

Examples: Input: arr[] = {1, 2}

Output: 4
(1, 1), (1, 2), (2, 1) (2, 2),

Input: arr[] = {1, 2, 3}

Output: 9
5. Calculate the sum of diagonal element of matrix.

support@hopeconflict.com
HOPECONFLICT TECHNOLOGY
 Home Work :
0) Write a Java program to sort a numeric array and a char array.

1) Write a Java program to test if an array contains a specific value

2) WAP to print reverse of an array using temp variable.

3) WAP to print reverse of an array without using temp variable.

4) Write a Java program to find the second largest element in an array

5) Write a Java program to add two matrices of the same size

6) WAP to put even and odd elements of array in two separate arrays.

7) Write a Java program to test the equality of two arrays

16) Write a Java program to get the difference between the largest and

smallest values in an array of integers.

17) WAP to replace all the 0’s with 1’s in your array. Your array is [26, 0,

67, 45, 0, 78, 54, 34, 0, 34].

18) Write a Java program to arrange the elements of an given array of

integers where all negative integers appear before all the positive
integers

19) WAP to sow the subtraction of two matrices.

20) WAP to print outer elements of 2D array of n X n..

21) WAP to show multiplication of two matrices.

22) Create Student class having rollno, name, marks. Create 10 objects .

Using Array of Objects display information of student who got highest


marks .

23) Create Class Employee (id,name,Salary).Craete 5 Employess Objects.

Display Employee information in descending order of Salary using


Array Of Objects.

24) Given 2 character arrays s1 and s2 and another empty character array s3.

support@hopeconflict.com
HOPECONFLICT TECHNOLOGY
Populate s3 by interleaving characters from both s1 and s1(Mindstix) Method
signature

Void interleaved (char[] s1, char[]s2, char[]s1, int s1_len, int s2_len)

/ Your Code

Example: S1={‘a’,’b’,’c’,’d’};
S2={‘w’,’x’,’y’,’z’};

Output: S1={‘a’,’w’’b’,’x’’c’,’y’,’d’,’z’}.

support@hopeconflict.com
HOPECONFLICT TECHNOLOGY

 Interview Questions :
a. What are arrays?

b. What is one dimensional array?

c. What is two dimensional array?

d. How to create and access elements in java?

e. What are features of array?

f. What are applications of an array?

g. What are advantages & disadvantages of arrays?

support@hopeconflict.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy