0% found this document useful (0 votes)
23 views

Array

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Array

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Q1*

{ public class f

{ public static void main (String[] args)

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

{ for (int i = 0; i < num.length; i++)

;System.out.print(num[i] + " ")

;)(System.out.println

{ for (int i = num.length-1; i >= 0; i--)

;System.out.print(num[i] + " ")

}
}
}
Q2*

{ public class f

{ public static void main (String[] args)

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

;System.out.println("The Even elements are : ")

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

};System.out.print(num[i] + " ")

;)(System.out.println

;System.out.println("The Odd elements are : ")

{for (int i = 0; i < num.length; i++)

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

};System.out.print(num[i] + " ")

}
}
}
Q3*

{ public class f

{ public static void main (String[] args)

;Scanner input = new Scanner(System.in)

; int [] num = new int [4]

;for (int i = 0; i < num.length; i++) { System.out.print("Enter num[" +i+ "]: ")

;)(num[i] = input.nextInt

{for (int i = 0; i < num.length; i++)

;System.out.print("num [" +i+ "]: " + num[i] +"\n")

;System.out.println(" num1 ")

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

for (int i = 0; i < num1.length; i++)

;System.out.print("num1 [" +i+ "]: "+num1[i]+"\n")

}
Q4*

]Forwarded from ÑŮM1[

{ public static void main (String[] args)

;Scanner input = new Scanner(System.in)

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

{ for(int i = 0; i < num.length; i++)

{ for(int j =i + 1; j < num.length; j++)

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

;System.out.println(num[j])

}
}
}
Q6*

;package num1.java

;import java.util.Arrays

{ public class f

{ public static void main(String[] args)

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

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

;int aLen = num1.length

;int bLen = num2.length

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

;System.arraycopy(num1, 0, num3, 0, aLen)

;System.arraycopy(num2, 0, num3, aLen, bLen)

;System.out.println(Arrays.toString(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 = num1.length

;int bLen = num2.length

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

;System.arraycopy(num1, 0, num3, 0, aLen)

;System.arraycopy(num2, 0, num3, aLen, bLen)

{ for (int i=0; i<num3.length-1; i++)

for (int j=i+1; j<num3.length; j++)

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

;x= num3[i]

;num3[i] = num3[j]

;num3[j] =x

}};System.out.println(Arrays.toString(num3)) }}
Q8[Forwarded from ÑŮM1]*

{ public class f

{ public static void main(String[] args)

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

;int [] num2 = new int [num1.length]

;int x= -1

{for(int i = 0; i < num1.length; i++)

;int c = 1

{for(int j = i+1; j <num1.length; 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 < num2.length; i++)}

if(num2[i] != x)

;System.out.println(num1[i] + " occurs " + num2[i]+" times")

}
}

}
Q9*

]Forwarded from ÑŮM1[

public class f

public static void main(String[] args)

;Scanner sc=new Scanner(System.in)

;int num1[]=new int[3]

;System.out.println("Enter elements in array")

;int min=Integer.MAX_VALUE

;int max=Integer.MIN_VALUE

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

;)(num1[i]=sc.nextInt

if(num1[i]<min)

;min=num1[i]

if(num1[i]>max)

;max=num1[i]

}
}

;System.out.println("Maximum element is = "+max)

;System.out.println("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(System.in)

;System.out.print("Enter n")

;)(n = scr.nextInt

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

;System.out.println("Enter all num1")

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

;)(a[i] = scr.nextInt

;)" ‫("في اي مكان تريد ادخال الرقم‬System.out.print

;)(b = scr.nextInt

;)"‫("اكتب الرقم الذي تريد ادخاله‬System.out.print

;)(x = scr.nextInt

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

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

;a[b-1] = x

;System.out.print("After inserting:")

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

;System.out.print(a[i]+",")

;System.out.print(a[n])
}
}

Q11*

]Forwarded from ÑŮM1[

;package num1.java

;import java.util.Scanner

public class f

public static void main(String[] args)

;int n,b, x

;Scanner scr = new Scanner(System.in)

;System.out.print("Enter n")

;)(n = scr.nextInt

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

;System.out.println("Enter all num1")

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

;)(num1[i] = scr.nextInt

;System.out.println("Enter the position of the number which is to be deleted")

;)(b= scr.nextInt

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

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

}
;n=n-1

;System.out.println("\n enter any num1 \n")

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

;System.out.println(" num1 ["+i+"] = "+ num1[i])

}
}
}
Q12*

]Forwarded from ÑŮM1[

;package num1.java

;*.import java.util

{class f

{ public static void main(String[] args)

{ =int[]num1

;}2,9,1,4,6

;Arrays.sort(num1)

;int x=num1.length-1

{while(num1[x]==num1[num1.length-1])

;--x

;System.out.println("Second largest value: " + num1[x])

}
Q13*

]Forwarded from ÑŮM1[

;package num1.java

;*.import java.util

{class f

{ public static void main(String[] args)

{ =int[]num1

;}0,9,4,6,5

;Arrays.sort(num1)

;int res = num1[1]

;System.out.println(" 2nd smallest element is :"+res)

}
}

You might also like