Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. package com.company;
  2. import java.io.ObjectStreamClass;
  3. import java.io.ObjectStreamField;
  4. import java.io.UnsupportedEncodingException;
  5. import java.nio.charset.Charset;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8.  
  9. public class Main {
  10.  
  11.     public static class MyString {
  12.         int count;
  13.         ArrayList<Object> value;
  14.  
  15.         public MyString() {
  16.             this.count = 0;
  17.             this.value = new ArrayList<Object>(0);
  18.         }
  19.  
  20.         public MyString(ArrayList<Object> value) {
  21.             int size = value.size();
  22.             this.count = size;
  23.             this.value = value;
  24.         }
  25.  
  26.         MyString(int count, ArrayList<Object> value) {//проверить на положительные числа
  27.             this.value = value;
  28.             this.count = count;
  29.         }
  30.  
  31.         public Object at(int place) { //проверить не выйдет ли из массива
  32.           return value.get(place);
  33.         }
  34.  
  35.         public int length() {
  36.             return count;
  37.         }
  38.  
  39.         public void add(Object a) {
  40.             value.add(a);
  41.             this.count++;
  42.         }
  43.  
  44.         public void insert(int i,Object a) {//добавить проверку на размер
  45.             this.value.add(i, a);
  46.             this.count++;
  47.         }
  48.  
  49.         public MyString substring(int beginIndex, int endIndex) {//проверить
  50.             if (beginIndex <  0) {
  51.                 throw new StringIndexOutOfBoundsException(beginIndex);
  52.             }
  53.             if (endIndex > count) {
  54.                 throw new StringIndexOutOfBoundsException(endIndex);
  55.             }
  56.             if (beginIndex > endIndex) {
  57.                 throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
  58.             }
  59.             return ((beginIndex == 0) && (endIndex == count)) ? this :
  60.                     new MyString( endIndex - beginIndex, new ArrayList<Object>(this.value.subList(beginIndex, endIndex+1)));
  61.         }
  62.  
  63.         public MyString substring(int endIndex) {//проверить
  64.             if (endIndex <  0) {
  65.                 throw new StringIndexOutOfBoundsException(endIndex);
  66.             }
  67.             if (endIndex > count) {
  68.                 throw new StringIndexOutOfBoundsException(endIndex);
  69.             }
  70.  
  71.             return (endIndex == count) ? this :
  72.                     new MyString( endIndex, new ArrayList<Object>(this.value.subList(0, endIndex)));
  73.         }
  74.  
  75.         public MyString(ArrayList<Object> value, int count) {
  76.             if (count < 0) {
  77.                 throw new StringIndexOutOfBoundsException(count);
  78.             }
  79.  
  80.             this.count = count;
  81.             this.value = new ArrayList<Object>(this.value.subList(0, count));
  82.  
  83.         }
  84.  
  85.        public MyString concat(MyString s) {//конкатенация строк
  86.  
  87.            MyString j = new MyString();
  88.             j.count = this.count + s.count;
  89.  
  90.            for(int i = 0; i< this.count;i++){
  91.                j.value.add(this.value.get(i));
  92.            }
  93.             for(int i = 0; i< s.count;i++){
  94.               j.value.add(s.value.get(i));
  95.            }
  96.             return j;
  97.  
  98.         }
  99.  
  100.        public void beginsWith(MyString s){
  101.            for(int i = 0; i< s.count;i++){
  102.                this.value.add(0,s.value.get(i));
  103.                this.count++;
  104.            }
  105.        }
  106.        public void endsWith(MyString s){
  107.            for(int i = 0; i< s.count;i++){
  108.                this.value.add(this.count,s.value.get(i));
  109.                this.count++;
  110.            }
  111.        }
  112.  
  113.         public boolean equals(MyString o) {
  114.  
  115.             if(this.count == o.count){
  116.                 for(int i = 0; i< o.count;i++)
  117.                 {
  118.                     if (o.value.get(i) != this.value.get(i)){
  119.                         return false;
  120.                     }
  121.                 }
  122.                 return true;
  123.             }
  124.             else
  125.             return false;
  126.         }
  127.     }
  128.  
  129.     public static void main(String[] args) {
  130.        
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement