0% found this document useful (0 votes)
2 views1 page

Filter Array for Specific Value in Java

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)
2 views1 page

Filter Array for Specific Value in Java

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

Q.

package [Link].mavenproject6;

import [Link];

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 < [Link]; i++) {

if (Array1[i] == 5) {

count+=1;

int[] Array2 = new int[count];

int count2 = 0;

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

if (Array1[i] == 5) {

Array2[count2] = Array1[i];

count2++;

[Link]("The old Array: " + [Link](Array1));

[Link]("The new Array with '5' only: " + [Link](Array2));

You might also like