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

Answer Java in Exam

The document describes a Java program that takes an integer array as input, counts the number of times the value 5 appears, creates a new array of that length, copies all elements equal to 5 into it, and prints out both arrays.
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)
6 views

Answer Java in Exam

The document describes a Java program that takes an integer array as input, counts the number of times the value 5 appears, creates a new array of that length, copies all elements equal to 5 into it, and prints out both arrays.
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/ 1

Q.

package com.mycompany.mavenproject6;

import java.util.Arrays;

public class Mavenproject6 {

public static void main(String[] args) {

int[] Array1 = {1, 5, 2, 6, 5, 4, 5, 6, 7, 8};

int count = 0;

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

if (Array1[i] == 5) {

count+=1;

int[] Array2 = new int[count];

int count2 = 0;

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

if (Array1[i] == 5) {

Array2[count2] = Array1[i];

count2++;

System.out.println("The old Array: " + Arrays.toString(Array1));

System.out.println("The new Array with '5' only: " + Arrays.toString(Array2));

You might also like