collection

collection

class Studnt{} public class StudentData { public static void main(String[] args) { List<Studnt> stuList=new ArrayList<Studnt>(); Studnt s1=new Studnt("name",70); Studnt s2=new Studnt("anil",54); Studnt s3=new Studnt("name",75); Studnt s4=new Studnt("name",45); Studnt s5=new Studnt("anil",74); Studnt s6=new Studnt("hi",74);

              stuList.add(s1);
              stuList.add(s2);
              stuList.add(s3);
              stuList.add(s4);
              stuList.add(s5);stuList.add(s6);

int sum=0; for(Studnt std:stuList){ for(Studnt std1:stuList){ String name1=std1.getName(); String name=std.getName(); int ma=std.getMarks();

                      if(name1.equals(name)){

                          sum=sum+ma;
                          ma=sum;
                         System.out.println(name+"::"+ma);

                         break;
                      }else{
                          System.out.println(name+"==="+ma);
                          break;
                      }

                  }
          }}

plz give me sol this type of out put anil::128 hi::74 name::190

View Answers

July 21, 2011 at 2:45 PM

import java.util.*;
class Studnt{

String name;
int marks;
Studnt(String name,int marks){
    this.name=name;
    this.marks=marks;
}
public String getName(){
return name;
}
public int getMarks(){
return marks;
}
}
public class StudentData{
    private static String[] getUniqueElements(String[] allStrs){
        String[] temp = new String[allStrs.length];
        int count = 0;
        for(int j = 0; j < allStrs.length; j++){
            if(isUnique(allStrs[j], temp))
                temp[count++] = allStrs[j];
        }
        String[] uniqueStrs = new String[count];
        System.arraycopy(temp, 0, uniqueStrs, 0, count);
        return uniqueStrs;
    } 
    private static boolean isUnique(String s, String[] array){
        for(int j = 0; j < array.length; j++){
            if(array[j] != null && s.equals(array[j]))
                return false;
        }
        return true;
    }
    public static void main(String[] args) {
                List<Studnt> stuList=new ArrayList<Studnt>();
                Studnt s1=new Studnt("name",70);
                Studnt s2=new Studnt("anil",54);
                Studnt s3=new Studnt("name",75); 
                Studnt s4=new Studnt("name",45); 
                Studnt s5=new Studnt("anil",74); 
                Studnt s6=new Studnt("hi",74);

              stuList.add(s1);
              stuList.add(s2);
              stuList.add(s3);
              stuList.add(s4);
              stuList.add(s5);
              stuList.add(s6);
              int i=0;
              String names[]=new String[stuList.size()];
              for(Studnt std:stuList){
                names[i]=std.getName();
                i++;
              }
              int sum=0;

            String name[]=getUniqueElements(names);
             int tot[]=new int[name.length];
            for(int k=0;k<name.length;k++){
                for(Studnt std:stuList){
                    if(std.getName().equals(name[k])){
                      sum+=std.getMarks();
                    }
                }
                tot[k]=sum;
            }
            for(int k=name.length-1;k>=0;k--){
            if(k==0){
                System.out.println(name[0]+" "+(tot[0]));
            }
            else{
                System.out.println(name[k]+" "+(tot[k]-tot[k-1]));

            }
            }
          }
}









Related Tutorials/Questions & Answers:
Collection
Collection  What is the exact difference between lagacy classes and collection classes? and Enumeration is possible on Collection classes
collection
collection  As we know array holds the similar kind of elements, then in collection how toArray() method will convert the collection having different objects as elements to an array in java
Advertisements
collection
, Hashtable and Collections and Collection?   Enumeration : It is series... : It is re-sizable array implementation. Belongs to 'List' group in collection... Map in collection. Collections : It implements Polymorphic algorithms which
Collection
Collection  actually why do we need collections? means we can also store group of objects in relational data base and dbms also provides all the operatoins insert,delete,update,sort,search etc. then why collection
collection overview and collection interface
collection overview and collection interface  write the program for collection interface?   Please visit the following links: http://www.roseindia.net/java/jdk6/Collection-Interfaces.shtml http://www.roseindia.net
Collection Types
Collection Types  What are the Collection types in Hibernate
Collection framework
Collection framework  what are the real life examples of using Collection in java
Java Collection
Java Collection  What is the Collection interface
Jaca Collection
Jaca Collection  does Java collection allow incompatible types to be stored in single collection
Collection in java
Collection in java  What are the meaning of the letters E,T,K and V that come in the collection concept
collection and framework.
collection and framework.  please give some study material of collection and framework
Collection Api
Collection Api  which scenario we are using arraylist and linkedlist and vector
collection interface
collection interface  methods of collection interface and their description with example program?   Please visit the following links: http://www.roseindia.net/java/jdk6/Collection-Interfaces.shtml http
collection interface
collection interface  methods of collection interface and their description with example program?   Please visit the following links: http://www.roseindia.net/java/jdk6/Collection-Interfaces.shtml http
COLLECTION FRAMEWORK
COLLECTION FRAMEWORK  Hi, i need complete detailed explanation on COLLECTION FRAMEWORK with examples(which include set,list,queue,map and operations performed on them).need help... Regards, Anu
COLLECTION FRAMEWORK
COLLECTION FRAMEWORK  Hi, i need complete detailed explanation on COLLECTION FRAMEWORK with examples(which include set,list,queue,map and operations performed on them).need help... Regards, Anu
Java collection
Java collection  What are differences between Enumeration, ArrayList, Hashtable and Collections and Collection
garbage collection
garbage collection  how to use garbage collection in program using core java
Collection Api
Collection Api  what is the use of navidableset and navigablemap why we are using that two class
Garbage collection
Garbage collection  why garbage collection? How it works?   Garbage collection is one of the most important features of Java. The purpose of garbage collection is to identify and discard objects that are no longer
Collection framework
Collection framework  import java.util.*; public class CollectFrame { public static void main(String [] args) { System.out.println( "Collection Example!\n" ); //int size; HashSet collection = new HashSet(); String str1
Collection HashMap
Collection HashMap  Please give me an example in which hash map having key value pair and key should be map again means map within map example. thanks in advance
Collection HashMap
Collection HashMap  Please give me an example in which hash map having key value pair and key should be map again means map within map example. thanks in advance
Collection Framework
Collection Framework  Please help me out..I have a class as below public class Employee { private int Id; private String name; private int salary; private int age; } public class Collection { public static void main
collection
collection
Collection
collection
collection
collection frame work
collection frame work  explain all the concept in collection frame work
c++ garbage collection implementation
c++ garbage collection implementation  How to implement the garbage collection program in C
collection frame work
collection frame work  explain the hierarchy of one dimensional collection frame work classes
collection frame work
collection frame work  could you please tell me detail the concept of collection frame work
What are the Collection types in Hibernate ?
What are the Collection types in Hibernate ?  hi, What are the Collection types in Hibernate ? Thanks
garbage collection in java
.style1 { color: #FFFFFF; } Garbage Collection in Java In java... through garbage collection. In other words it deallocate memory automatically which have no longer use. This technique is known as Garbage Collection
Java garbage collection
Java garbage collection  What is Garbage Collection and how to call it explicitly
Collection framework tutorial
Collection framework tutorial  Hi, I am learning Collection framework in Java. I want many examples of Java collection framework. Tell me the best tutorial website for learning Java Collection framework. ThanksADS_TO_REPLACE_1
Collection Framework - Java Beginners
Collection Framework  Pls explain the disadvantages of using collection framework as it was written in this forum ? It must cast to correct type. ? Cannot do compile time checking. I couldnot get in detail as to what
on collection mapping - Hibernate
on collection mapping  differences b/w collection mappings tags list,set,bag   Hi Friend, Differences: A set is a collection in which no item occurs more than once. A bag is a collection in which items may
Sorted and ordered collection
Sorted and ordered collection  What is the difference between sorted and ordered collection in hibernate
Collection classes in java
Collection classes in java   Normally a database is used... is the reason using java collection classes saved/stored the data/content.I don't understand, what is the idea using java collection classes in project
Collection classes in java
Collection classes in java   Normally a database is used... is the reason using java collection classes saved/stored the data/content.I don't understand, what is the idea using java collection classes in project
Anthem DVD Collection
Anthem DVD Collection       DVD collection based on the Ajax Anthem.Net project Read full descriptionADS_TO_REPLACE_1
Collection : ArrayList Example
This tutorial contains description of Collection ArrayList with example
Collection : LinkedList Example
This tutorial contains description of Collection LinkedList with example
struts collection problem in Mozilla
struts collection problem in Mozilla   I used the to get list of records, if records are more than 15-20 then this is not seen properly with vertical scroll bar on Mozilla. Plz help me
Filter collection in Java 8
Filter collection in Java 8  Hi, I have following collection..., Example of Filter collection in Java 8 You can use the following code in Java 8... the data not containing "Java". Thanks   Hi, We have huge collection
Array to Collection
Array to Collection       In this example we are converting values of an array into collection. List  interface is a member of the Java Collection Framework
garbage collection implementation dependent java
garbage collection implementation dependent java  How a garbage collection works in Java
Java Collection API - Java Tutorials
Java Collection API Collection was added to Java with J2SE 1.2 release. Collection framework is provided in 'java.util.package'. All collections... implementations of the collection interfaces. Also, they are reusable data structures

Ads