0% found this document useful (0 votes)
37 views13 pages

Java Array Manipulation Examples

The document contains 10 code snippets showing the use of arrays in Java programs. The snippets demonstrate initializing arrays, iterating through arrays, copying arrays, sorting arrays, finding minimum and maximum elements, and inserting elements into arrays.

Uploaded by

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

Java Array Manipulation Examples

The document contains 10 code snippets showing the use of arrays in Java programs. The snippets demonstrate initializing arrays, iterating through arrays, copying arrays, sorting arrays, finding minimum and maximum elements, and inserting elements into arrays.

Uploaded by

Nora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q1*

{ public class f

{ public static void main (String[] args)

;int [] num = new int [] {2, 5,7}

{ for (int i = 0; i < [Link]; i++)

;[Link](num[i] + " ")

;)([Link]

{ for (int i = [Link]-1; i >= 0; i--)

;[Link](num[i] + " ")

}
}
}
Q2*

{ public class f

{ public static void main (String[] args)

;int [] num = new int [] {25, 47,42,56,32}

;[Link]("The Even elements are : ")

{for (int i = 0; i < [Link]; i++) { if(num[i]%2==0)

};[Link](num[i] + " ")

;)([Link]

;[Link]("The Odd elements are : ")

{for (int i = 0; i < [Link]; i++)

{if(num[i]%2==1)

};[Link](num[i] + " ")

}
}
}
Q3*

{ public class f

{ public static void main (String[] args)

;Scanner input = new Scanner([Link])

; int [] num = new int [4]

;for (int i = 0; i < [Link]; i++) { [Link]("Enter num[" +i+ "]: ")

;)(num[i] = [Link]

{for (int i = 0; i < [Link]; i++)

;[Link]("num [" +i+ "]: " + num[i] +"\n")

;[Link](" num1 ")

; int [] num1 = new int []{num[1],num[3]}

for (int i = 0; i < [Link]; i++)

;[Link]("num1 [" +i+ "]: "+num1[i]+"\n")

}
Q4*

]Forwarded from ÑŮM1[

{ public static void main (String[] args)

;Scanner input = new Scanner([Link])

;int [] num = new int []{5,1,1,6,8,6}

{ for(int i = 0; i < [Link]; i++)

{ for(int j =i + 1; j < [Link]; j++)

if(num[i] == num[j])

;[Link](num[j])

}
}
}
Q6*

;package [Link]

;import [Link]

{ public class f

{ public static void main(String[] args)

;int[] num1 = {1, 2, 3}

;int[] num2 = {6,4, 2,3}

;int aLen = [Link]

;int bLen = [Link]

;int[] num3 = new int[aLen + bLen]

;[Link](num1, 0, num3, 0, aLen)

;[Link](num2, 0, num3, aLen, bLen)

;[Link]([Link](num3))

}
Q7*

]Forwarded from ÑŮM1[

{ public static void main(String[] args)

;int[] num1 = {1, 2, 3}

;int[] num2 ={1, 2, 3}

;int x

;int aLen = [Link]

;int bLen = [Link]

;int[] num3 = new int[aLen + bLen]

;[Link](num1, 0, num3, 0, aLen)

;[Link](num2, 0, num3, aLen, bLen)

{ for (int i=0; i<[Link]-1; i++)

for (int j=i+1; j<[Link]; j++)

if ( num3[i] > num3[j] )

;x= num3[i]

;num3[i] = num3[j]

;num3[j] =x

}};[Link]([Link](num3)) }}
Q8[Forwarded from ÑŮM1]*

{ public class f

{ public static void main(String[] args)

;int[] num1 = {25,12,43}

;int [] num2 = new int [[Link]]

;int x= -1

{for(int i = 0; i < [Link]; i++)

;int c = 1

{for(int j = i+1; j <[Link]; j++)

{if(num1[i] == num1[j])

;++c

To avoid counting same element again //

;num2[j] = x

}
}

if(num2[i] != x)

;num2[i] =c

{for(int i = 0; i < [Link]; i++)}

if(num2[i] != x)

;[Link](num1[i] + " occurs " + num2[i]+" times")

}
}

}
Q9*

]Forwarded from ÑŮM1[

public class f

public static void main(String[] args)

;Scanner sc=new Scanner([Link])

;int num1[]=new int[3]

;[Link]("Enter elements in array")

;int min=Integer.MAX_VALUE

;int max=Integer.MIN_VALUE

for(int i=0;i<3;i++)

;)(num1[i]=[Link]

if(num1[i]<min)

;min=num1[i]

if(num1[i]>max)

;max=num1[i]

}
}

;[Link]("Maximum element is = "+max)

;[Link]("Minimum element is = "+min)

}
Q10*

]Forwarded from ÑŮM1[

public class f

public static void main(String[] args)

;int n,b, x

;Scanner scr = new Scanner([Link])

;[Link]("Enter n")

;)(n = [Link]

;int a[] = new int[n+1]

;[Link]("Enter all num1")

for(int i = 0; i < n; i++)

;)(a[i] = [Link]

;)" ‫("في اي مكان تريد ادخال الرقم‬[Link]

;)(b = [Link]

;)"‫("اكتب الرقم الذي تريد ادخاله‬[Link]

;)(x = [Link]

for(int i = (n-1); i >= (b-1); i--)

;a[i+1] = a[i]

;a[b-1] = x

;[Link]("After inserting:")

for(int i = 0; i < n; i++)

;[Link](a[i]+",")

;[Link](a[n])
}
}

Q11*

]Forwarded from ÑŮM1[

;package [Link]

;import [Link]

public class f

public static void main(String[] args)

;int n,b, x

;Scanner scr = new Scanner([Link])

;[Link]("Enter n")

;)(n = [Link]

;int num1[] = new int[n+1]

;[Link]("Enter all num1")

for(int i = 0; i < n; i++)

;)(num1[i] = [Link]

;[Link]("Enter the position of the number which is to be deleted")

;)(b= [Link]

for(int i=b;i<n-1;i++)

;num1[i]= num1[i+1]

}
;n=n-1

;[Link]("\n enter any num1 \n")

for(int i=0;i<n;i++)

;[Link](" num1 ["+i+"] = "+ num1[i])

}
}
}
Q12*

]Forwarded from ÑŮM1[

;package [Link]

;*.import [Link]

{class f

{ public static void main(String[] args)

{ =int[]num1

;}2,9,1,4,6

;[Link](num1)

;int x=[Link]-1

{while(num1[x]==num1[[Link]-1])

;--x

;[Link]("Second largest value: " + num1[x])

}
Q13*

]Forwarded from ÑŮM1[

;package [Link]

;*.import [Link]

{class f

{ public static void main(String[] args)

{ =int[]num1

;}0,9,4,6,5

;[Link](num1)

;int res = num1[1]

;[Link](" 2nd smallest element is :"+res)

}
}

You might also like