Rahmadnet

Array

Feb 28th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. package stateBefore_After;
  2.  
  3. public class Sentence implements Comparable<Sentence> {
  4.     private String[] arr;
  5.     private int max_length;
  6.     private int index;
  7.  
  8.     public Sentence(String sentence) {
  9.         this.arr = sentence.split("\\s+");
  10.         this.max_length = arr[0].length();
  11.         for(int i = 0; i < arr.length; i++) {
  12.             if (max_length < arr[i].length()) {
  13.                 this.max_length = arr[i].length();
  14.                 this.index = i;
  15.             }
  16.         }
  17.     }
  18.  
  19.     public String[] getArr() {
  20.         return arr;
  21.     }
  22.  
  23.     public int getMax_length() {
  24.         return max_length;
  25.     }
  26.  
  27.     public int compareTo(Sentence sentence) {
  28.         if (max_length == sentence.getMax_length())
  29.             return 0;
  30.         else if (max_length < sentence.getMax_length())
  31.             return -1;
  32.         else if (max_length > sentence.getMax_length())
  33.             return 1;
  34.         else
  35.             return max_length;
  36.     }
  37.  
  38.     public String toString() {
  39.         StringBuilder builder = new StringBuilder();
  40.         for (int i = 0; i < arr.length; i++) {
  41.             builder.append(arr[i] + " ");
  42.         }
  43.         return builder.toString();
  44.     }
  45.  
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment