Create a generic method for counting the number of times that a given item occurs in an array. The method accepts an array and an item to find in the array. These are a generic data type. The method returns an integer. Here is an example of calling the method.

If wordList is a variable of type String[] and word is a variable of type String

int count = countOccurrences(wordList, word);

Test your method using an array of String and an array of Integer (not int, can’t use primitive types with generics).

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public static void main(String[] args)
String[] test = {"test1", "test2", "test1", "test3", "test4", "test5", "test1"};
int occCount = countOccurrences(test, "test1");

System.out.println("Test 1 occured " + occCount + " times in the array");

Integer[] test2 = {1, 4, 5, 24, 53, 23, 53, 24, 59, 35 ,24, 43, 53, 26, 24};
occCount = countOccurrences(test2, 53);

System.out.println("53 occured " + occCount + " times in the array");

By purchasing this solution you'll be able to access the following files:

50% discount

$20.00 $10.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats