Advertisement
Guest User

ArrayList Worksheet #3

a guest
Apr 7th, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         // write your code here
  9.  
  10.     }
  11.  
  12.     public static int addLenghtsExceptS(ArrayList<String> list)
  13.     {
  14.         int length = 0;
  15.         for (int i = 0; i < list.size(); i++)
  16.         {
  17.             if (!list.get(i).startsWith("s"))
  18.             {
  19.                 length += list.get(i).length();
  20.             }
  21.         }
  22.     }
  23.  
  24.     public static String findMin(ArrayList<Person> list) {
  25.         Person youngestPerson = new Person("Jim". 0);
  26.         //Assuming this object has a public constructor for this solution.
  27.         //If the object does not, a different solution can be found below.
  28.  
  29.  
  30.         for (int i = 0; i < list.size(); i++)
  31.         {
  32.             if (list.get(i).getAge() < youngestPerson.getAge())
  33.             {
  34.                 //Reference type. Pointer to the original object created in the list.
  35.                 youngestPerson = list.get(i);
  36.             }
  37.         }
  38.         return youngestPerson.getName();
  39.     }
  40.  
  41.  
  42.     public static String findMin(ArrayList<Person> list) {
  43.         int youngestAge;
  44.         int youngestName;
  45.         //Assuming this object has a public constructor for this solution.
  46.         //If the object does not, a different solution can be found below.
  47.  
  48.  
  49.         for (int i = 0; i < list.size(); i++)
  50.         {
  51.             if (list.get(i).getAge() < youngestAge)
  52.             {
  53.                 //Reference type. Pointer to the original object created in the list.
  54.                 youngestAge = list.get(i).getAge();
  55.                 youngestName = list.get(i).getName();
  56.             }
  57.         }
  58.         return youngestName;
  59.     }
  60.  
  61.     public static void removeFirstLettersAndInsertFun(ArrayList<String> list)
  62.     {
  63.         for(int i = 0; i < list.size(); i++)
  64.         {
  65.             list.set(i, list.get(i).substring(1));
  66.         }
  67.         //Arraylist has a generic override of the add() method that permits specifying an index.
  68.         //Subtract 2 because the list indicies start at zero, and you also want the second to last element.
  69.         list.add(list.size() - 2,"fun");
  70.     }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement