Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Find in Array - Calculate Occurrences of Elements in Array 
 

This section illustrates you how to calculate occurrences of elements in an array.

 

Find in Array - Calculate Occurrences of Elements in Array

                         

This section illustrates you how to calculate occurrences of elements in an array. Occurrences means, the how many times an element occurs in the array. In this section, you will get more and more help from the given following program with the complete code.

Program Description:

This program first asks for the length of the array whatever you want to fix and then it takes some inputs for the elements of the array how more you have mentioned for the length of the array. And finally it show the occurrences of each and every element of the array uniquely.

Here is the code of the program:

import java.io.*;

public class OccurancesInArray{
  public static void main(String[] argsthrows IOException{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("How many elements you want to enter in the array: ");
    int num=0;
    try{
      num = Integer.parseInt(in.readLine());
    }
    catch(NumberFormatException ne){
      System.out.println(ne.getMessage() " is not a number!");
      System.exit(0);
    }
    String[] elements = new String[num];
    int a;
    int k;
    for(int i = 0; i < num; i++){
      elements[i= in.readLine();
    }
    for(int i = 0; i < elements.length; i++){
      a = 0;
      k = 1;
      for(int j = 0; j < elements.length; j++){
        if(j >= i){
          if(elements[i].equals(elements[j]) && j != i){
            k++;
          }
        }
        else if(elements[i].equals(elements[j])){
          a = 1;
        }
      }
      if(a != 1){
        System.out.println("Occurance of \'" + elements[i"\' : " + k);
      }
    }
  }
}

Output of the program :

C:\chandan>javac OccurancesInArray.java

C:\chandan>java OccurancesInArray
How many elements you want to enter in the array: 5
123
chand
453
aaa
123
Occurance of '123' : 2
Occurance of 'chand' : 1
Occurance of '453' : 1
Occurance of 'aaa' : 1

C:\work\chandan>_

Download this example.

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

please correct the error from this program.

public class Add
{
public static void main(String[]args)
{

int a[][]={{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1}};
int b[][]={{2,2,2,2,2},{2,2,2,2,2},{2,2,2,2,2},{2,2,2,2,2},{2,2,2,2,2}};
calculate(a,b);

}

public static int calculate(int [][]a ,int [][]b);
{

int a[][]={{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1},{1,1,1,1,1}};
int b[][]={{2,2,2,2,2},{2,2,2,2,2},{2,2,2,2,2},{2,2,2,2,2},{2,2,2,2,2}};
int sum;
for(int x=0; x<=5; x++){
for(int y=0; y<=5; y++){
{
sum=a[x][y]+b[x][y];
}
}
}
System.out.println("The sum is:"+sum);

}
}





Posted by diana on Wednesday, 03.5.08 @ 09:50am | #51380

how to get the answer
adding two matrics

write 2 methods to add 2 matrics. the method header is as follows:

public static int[][] addMatrix(int [][]a, int[][]b).

the 2 matrix must have same dimension and the same or compatible types of elements.

Posted by diana on Monday, 02.25.08 @ 23:04pm | #49982

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.