Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. package com.example.android.myapplication;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6.  
  7. import java.util.ArrayList;
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15.  
  16. ArrayList<Student> students = new ArrayList<>();
  17. students.add(new Student("John", 20));
  18. students.add(new Student("Jim", 15));
  19. students.add(new Student("Ann", 18));
  20. students.add(new Student("John", 14));
  21.  
  22. ArrayList<Student> studentsSumTotal = new ArrayList<>();
  23.  
  24. String prevStudentName = students.get(0).name;
  25. int prevStudentScore = students.get(0).score;
  26.  
  27. for (int i = 1; i < students.size(); i++) {
  28.  
  29. String currentName = students.get(i).name;
  30. int currentScore = students.get(i).score;
  31.  
  32. if (currentName == prevStudentName) {
  33. int totalScore = currentScore + prevStudentScore;
  34. studentsSumTotal.add(new Student(currentName, totalScore));
  35. } else {
  36. studentsSumTotal.add(new Student(currentName, currentScore));
  37. }
  38. }
  39.  
  40.  
  41. Log.v("=================== Msg", "Sum of Score ===================");
  42. // Display Sum of Score for each student
  43. for (int i = 0; i < studentsSumTotal.size(); i++) {
  44. Log.v(studentsSumTotal.get(i).name, studentsSumTotal.get(i).score + "");
  45. }
  46. Log.v("=================== Msg", "Sum of Score ===================");
  47. Log.v("Size of studio", studentsSumTotal.size() + "");
  48.  
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement