answersLogoWhite

0


Best Answer

The below is an example to find the instance of a string in a string array. You can modify this to any kind of array and also pass the array and value to be checked as parameters.

public class ArrayTest {

/**

* @param args

*/

public static void main(String[] args) {

String[] strArr = new String[] {"One", "Two", "Three", "Four", "One"};

String txt = "One";

int size = strArr.length;

int counter = 0;

for(int i = 0; i < size; i++){

String temp = (String) strArr[i];

if(temp.trim().equals(txt)){

counter++;

}

}

System.out.println("The word '" + txt + "' occurs " + counter + " Times in the array");

}

}

The output of this program would be:

The world 'One' occurs 2 Times in the array

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: No of occurrence of element in an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How arrays are used in synon?

Synon array processing is totally intuitive for any synon user. You do not need to open a manual. If you can use a file, you can use an array. The main difference is that there are only 4 function types: CRTOBJ, CHGOBJ, DLTOBJ, RTVOBJ, and only 1 "ACP" (set of Key fields)The array processing really behaves more like an indexed file than an array. The file portion (Records) is a multiple occurrence data structure and the index portion is an array. The data structure contains the fields as defined in the *ARRAY file. The array(Index) is the fields defined as the Key concatenated with the occurrence# of the data structure. When you create an element in the array (a record in the file), the program takes the next available occurrence of the data structure and loads the fields. The program also creates the Index element by concatenating the key fields with the occurrence# of the newly added data structure. The program then does a Sort Array to sequence the Index so that the records may be accessed in key sequence. Any time a record is deleted, the Index element is blanked out and the Index is Sorted (Sort Array). This is a very clean solution and the performance is surprisingly good considering that the array is sorted after each add or delete.When accessing the data, a LOKUP is performed on the array using the key value with blanks instead of the occurrence# and looking for GT in the array. This allows processing of partial keys as positioners or restrictors. The program then reads the data structure for the occurrence# taken from the array element to get the data(fields). For the next occurrence, it is not necessary to do another LOKUP (which is slow) because the array is sorted. The program only has to read the next element in the array and continue the processing as long as the partial key matches the key in the array. From the point of view of the Synon user, this processing is identical to a file RTVOBJ.Synon loads the array from the bottom (an RPG technique to improve performance in large arrays) because the LOKUP command may be given a starting position, but cannot be stopped until every element is checked when no match is found. By loading the array backwards from the bottom and counting the elements, the search may be started at the last element loaded and only elements which contain data are read.IF No Match is FoundAn array is defined for 1000 elements.Only 10 elements are loaded.These are loaded to occurrences 991 thru 1000.When the LOKUP is performed, it starts at 991 and only 10 compares are made.** If the array is loaded from the top, 1000 compares are performed.


Will an array element be deleted when you retrieve it from the array?

You cannot delete from an array.


Which element of array does this expression refer num5?

which element of the array does this expression reference num[5]


What is the index number of the last element of an array with 9 elements?

(array.length - 1) will find the index of the last element in an array (or -1 if the array is empty).


How do you represent binary tree using array?

The root of the tree is stored in array element [0]; for any node of the tree that is stored in array element [i], its left child is stored in array element [2*i], its right child at [2*i+2]

Related questions

How arrays are used in synon?

Synon array processing is totally intuitive for any synon user. You do not need to open a manual. If you can use a file, you can use an array. The main difference is that there are only 4 function types: CRTOBJ, CHGOBJ, DLTOBJ, RTVOBJ, and only 1 "ACP" (set of Key fields)The array processing really behaves more like an indexed file than an array. The file portion (Records) is a multiple occurrence data structure and the index portion is an array. The data structure contains the fields as defined in the *ARRAY file. The array(Index) is the fields defined as the Key concatenated with the occurrence# of the data structure. When you create an element in the array (a record in the file), the program takes the next available occurrence of the data structure and loads the fields. The program also creates the Index element by concatenating the key fields with the occurrence# of the newly added data structure. The program then does a Sort Array to sequence the Index so that the records may be accessed in key sequence. Any time a record is deleted, the Index element is blanked out and the Index is Sorted (Sort Array). This is a very clean solution and the performance is surprisingly good considering that the array is sorted after each add or delete.When accessing the data, a LOKUP is performed on the array using the key value with blanks instead of the occurrence# and looking for GT in the array. This allows processing of partial keys as positioners or restrictors. The program then reads the data structure for the occurrence# taken from the array element to get the data(fields). For the next occurrence, it is not necessary to do another LOKUP (which is slow) because the array is sorted. The program only has to read the next element in the array and continue the processing as long as the partial key matches the key in the array. From the point of view of the Synon user, this processing is identical to a file RTVOBJ.Synon loads the array from the bottom (an RPG technique to improve performance in large arrays) because the LOKUP command may be given a starting position, but cannot be stopped until every element is checked when no match is found. By loading the array backwards from the bottom and counting the elements, the search may be started at the last element loaded and only elements which contain data are read.IF No Match is FoundAn array is defined for 1000 elements.Only 10 elements are loaded.These are loaded to occurrences 991 thru 1000.When the LOKUP is performed, it starts at 991 and only 10 compares are made.** If the array is loaded from the top, 1000 compares are performed.


Will an array element be deleted when you retrieve it from the array?

You cannot delete from an array.


How do you find particular element in array?

by using index position we can find the particular element in array.


Which element of array does this expression refer num5?

which element of the array does this expression reference num[5]


What is the index number of the last element of an array with 9 elements?

(array.length - 1) will find the index of the last element in an array (or -1 if the array is empty).


How do you represent binary tree using array?

The root of the tree is stored in array element [0]; for any node of the tree that is stored in array element [i], its left child is stored in array element [2*i], its right child at [2*i+2]


What search begin the search with the first array element?

The search for the first array element begins at the assembly plant. As they array is being constructed, the element itself is one of the first components to be completed.


How do you return an array from function?

By returning a pointer to the first element of the array.


How will you find the location of an element of an array?

Basically, &amp;array[i]; That is, the memory location for an array object with index i. Or, you can do: (array + i);


The contents of a particular element of an array is called?

It is the value of the element.


How do you write a C plus plus function lastLargestIndex that returns the index of the last occurrence of the largest element in the array?

int lastLargestIndex(int a[],int n) //a=array, n= number of elements in array { int max=a[0],maxp=0; //max=largest no., maxp= position of largest no. for(int i=0;i&lt;n;i++) if(a[i]&gt;=max) { max=a[i]; maxp=i; } return maxp; }


How to display the content of memory address stored in an element of an array?

void *array[2]; printf ("array[%d]=%p\n", i, array[i]);