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

Java Array Element Manipulation

The document provides Java code examples for manipulating arrays, including deleting and updating elements in a one-dimensional array, as well as reading and writing matrix elements in a two-dimensional array. It also includes functionality for adding and multiplying matrices, with user input for matrix dimensions and elements. The output demonstrates the results of these operations with sample matrices.

Uploaded by

Rehan Hussain
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

Java Array Element Manipulation

The document provides Java code examples for manipulating arrays, including deleting and updating elements in a one-dimensional array, as well as reading and writing matrix elements in a two-dimensional array. It also includes functionality for adding and multiplying matrices, with user input for matrix dimensions and elements. The output demonstrates the results of these operations with sample matrices.

Uploaded by

Rehan Hussain
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

DURGASOFT

static int[] deleteElement(int a[],int element)


{
int index=-1,i,k=0;
for(i=0;i<[Link];i++)
{
if(a[i]==element)
{
index=i;
break;
}
}
if(index!=-1)
{
int b[] = new int[[Link]-1];
for(i=0;i<[Link];i++)
{
if(i==index)
continue;
b[k++]=a[i];
}
return b;
}
return a;
}
}
class Test
{
public static void main(String[] args)
{
int a[] = {10,11,12,13,14,15};
[Link]([Link](a));
[Link]([Link]([Link](a,10)));
[Link]([Link]([Link](a,11)));
[Link]([Link]([Link](a,12)));
[Link]([Link]([Link](a,13)));
[Link]([Link]([Link](a,14)));
[Link]([Link]([Link](a,15)));

}
}

output:
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
141  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
-------
[10, 11, 12, 13, 14, 15]
[11, 12, 13, 14, 15]
[10, 12, 13, 14, 15]
[10, 11, 13, 14, 15]
[10, 11, 12, 14, 15]
[10, 11, 12, 13, 15]
[10, 11, 12, 13, 14]

UPDATING ELEMENT IN AN ARRAY


----------------------------
case1: updating based on location
----------------------------------
import [Link].*;

class Demo
{
static int[] updateElementAtLocation(int a[],int location,int element)
{
int b[] = new int[[Link]];
for(int i=0;i<[Link];i++)
b[i] = a[i];
if(location>=0 && location<[Link])
b[location]=element;
return b;
}
}
class Test
{
public static void main(String[] args)
{
int a[] = {10,11,12,13,14,15};
[Link]([Link](a));

[Link]([Link]([Link](a,0,999)));

[Link]([Link]([Link](a,1,999)));

[Link]([Link]([Link](a,2,999)));

[Link]([Link]([Link](a,3,999)));

DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,


142  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
[Link]([Link]([Link](a,4,999)));

[Link]([Link]([Link](a,5,999)));

[Link]([Link]([Link](a,7,999)));
}
}

output:
-------
[10, 11, 12, 13, 14, 15]
[999, 11, 12, 13, 14, 15]
[10, 999, 12, 13, 14, 15]
[10, 11, 999, 13, 14, 15]
[10, 11, 12, 999, 14, 15]
[10, 11, 12, 13, 999, 15]
[10, 11, 12, 13, 14, 999]
[10, 11, 12, 13, 14, 15]

case2: update based on elemenet:


--------------------------------
import [Link].*;

class Demo
{
static int[] updateElementAtLocation(int a[],int location,int element)
{
int b[] = new int[[Link]];
for(int i=0;i<[Link];i++)
b[i] = a[i];
if(location>=0 && location<[Link])
b[location]=element;
return b;
}
static int[] updateElement(int a[],int oldElement,int newElement)
{
int i,b[] = new int[[Link]];
for(i=0;i<[Link];i++)
b[i] = a[i];
for(i=0;i<[Link];i++)
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
143  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
{
if(b[i]==oldElement)
{
b[i] = newElement;
break;
}
}
return b;
}
}
class Test
{
public static void main(String[] args)
{
int a[] = {10,11,12,13,14,15};
[Link]([Link](a));
[Link]([Link]([Link](a,10,999)));
[Link]([Link]([Link](a,90,999)));
}
}

output:
-------
[10, 11, 12, 13, 14, 15]
[999, 11, 12, 13, 14, 15]
[10, 11, 12, 13, 14, 15]

two-d arrays:
--------------
==> row and cols
==> two-d arrays not implemented in 'array of arrays' style

Implement a program to read and write matrix element


----------------------------------------------------
import [Link].*;

class Test
{
public static void main(String[] args)
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
144  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
Scanner obj = new Scanner([Link]);

[Link]("Enter matrix row size:");


int rsize = [Link]();

[Link]("Enter matrix column size:");


int csize = [Link]();

int i,j,a[][] = new int[rsize][csize];

[Link]("Enter matrix element one-by-one:");


for(i=0;i<rsize;i++)
{
for(j=0;j<csize;j++)
{
a[i][j] = [Link]();
}
}

[Link]("MATRIX ELEMENTS ARE:");


for(i=0;i<rsize;i++)
{
for(j=0;j<csize;j++)
{
[Link](a[i][j]+"["+i+","+j+"] ");
}
[Link]();
}
}
}

output:
-------
C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
Enter matrix row size:
3
Enter matrix column size:
3
Enter matrix element one-by-one:
123
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
145  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
456
789
MATRIX ELEMENTS ARE:
1[0,0] 2[0,1] 3[0,2]
4[1,0] 5[1,1] 6[1,2]
7[2,0] 8[2,1] 9[2,2]

Implement a program to perform addition of two matrices


-------------------------------------------------------
import [Link].*;

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);

[Link]("Enter matrix-A row size:");


int rsize1 = [Link]();

[Link]("Enter matrix-A column size:");


int csize1 = [Link]();

[Link]("Enter matrix-B row size:");


int rsize2 = [Link]();

[Link]("Enter matrix-B column size:");


int csize2 = [Link]();

if(rsize1==rsize2 && csize1==csize2)


{
int i,j;
int a[][] = new int[rsize1][csize1];
int b[][] = new int[rsize2][csize2];
int c[][] = new int[rsize1][csize1];

[Link]("Enter matrix-A element one-by-one:");


for(i=0;i<rsize1;i++)
{
for(j=0;j<csize1;j++)
{
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
146  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
a[i][j] = [Link]();
}
}

[Link]("Enter matrix-B element one-by-one:");


for(i=0;i<rsize2;i++)
{
for(j=0;j<csize2;j++)
{
b[i][j] = [Link]();
}
}

for(i=0;i<rsize1;i++)
{
for(j=0;j<csize1;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}

[Link]("MATRIX-A ELEMENTS ARE:");


for(i=0;i<rsize1;i++)
{
for(j=0;j<csize1;j++)
{
[Link](a[i][j]+" ");
}
[Link]();
}

[Link]("MATRIX-B ELEMENTS ARE:");


for(i=0;i<rsize2;i++)
{
for(j=0;j<csize2;j++)
{
[Link](b[i][j]+" ");
}
[Link]();
}

[Link]("MATRIX-C ELEMENTS ARE:");


for(i=0;i<rsize1;i++)
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
147  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
{
for(j=0;j<csize1;j++)
{
[Link](c[i][j]+" ");
}
[Link]();
}
}
else
{
[Link]("MATRIX addition is not possible");
}
}
}

output:
-------
C:\prakashclasses>javac [Link]

C:\prakashclasses>java Test
Enter matrix-A row size:
3
Enter matrix-A column size:
3
Enter matrix-B row size:
4
Enter matrix-B column size:
5
MATRIX addition is not possible

C:\prakashclasses>java Test
Enter matrix-A row size:
3
Enter matrix-A column size:
3
Enter matrix-B row size:
3
Enter matrix-B column size:
3
Enter matrix-A element one-by-one:
123
456
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
148  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
789
Enter matrix-B element one-by-one:
123
456
789
MATRIX-A ELEMENTS ARE:
123
456
789
MATRIX-B ELEMENTS ARE:
123
456
789
MATRIX-C ELEMENTS ARE:
246
8 10 12
14 16 18

multiplication:
---------------
scalar matrix multiplication
two matrix multiplication

Ex:
123
456
789

246
8 10 12
14 16 18

Ex:
import [Link].*;

class Test
{
public static void main(String[] args)
{
Scanner obj = new Scanner([Link]);

DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,


149  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]
DURGASOFT
[Link]("Enter matrix-A row size:");
int rsize1 = [Link]();

[Link]("Enter matrix-A column size:");


int csize1 = [Link]();

[Link]("Enter matrix-B row size:");


int rsize2 = [Link]();

[Link]("Enter matrix-B column size:");


int csize2 = [Link]();

if(rsize1==csize2)
{
int i,j,k;
int a[][] = new int[rsize1][csize1];
int b[][] = new int[rsize2][csize2];
int c[][] = new int[rsize1][csize1];

[Link]("Enter matrix-A element one-by-one:");


for(i=0;i<rsize1;i++)
{
for(j=0;j<csize1;j++)
{
a[i][j] = [Link]();
}
}

[Link]("Enter matrix-B element one-by-one:");


for(i=0;i<rsize2;i++)
{
for(j=0;j<csize2;j++)
{
b[i][j] = [Link]();
}
}

for(i=0;i<rsize1;i++)
{
for(j=0;j<csize2;j++)
{
c[i][j] = 0;
DURGASOFT, # 202, 2nd Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
150  88 85 25 26 27, 72 07 21 24 27/28 | [Link]
Maii: durgasoftonline@[Link]

You might also like