Skip to content

Instantly share code, notes, and snippets.

@YanchevskayaAnna
Created June 10, 2016 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YanchevskayaAnna/47ca94f8b5697de6c7b01b4df75dc5b5 to your computer and use it in GitHub Desktop.
Save YanchevskayaAnna/47ca94f8b5697de6c7b01b4df75dc5b5 to your computer and use it in GitHub Desktop.
package homeWork.myString;
/**
* Created by mykhailov on 29.05.2016.
*/
public class MyString {
private char[] chars;
private int length;
public MyString() {
}
public MyString(String str) {
length = str.length(); //Yanchevskaya A. Нет проверки на null
if (str.length() == 0) return; //Yanchevskaya A.Без проверки метод = str.toCharArray() не отработает?
chars = str.toCharArray();
}
public MyString(char[] ch) {
if (ch == null) return;
int count = ch.length;
chars = ch;
System.arraycopy(ch, 0, chars, 0, count); // Yanchevskaya A. массив уже заполнен из предыдущей команды
}
public String toString() {
return new String(chars); // Yanchevskaya A. вообще по-хорошему в этом задании нельзя использовать методы String, нужен цикл
}
public char charAt(int index) {
if ((index < 0) || (index >= chars.length))
throw new StringIndexOutOfBoundsException(index);
return chars[index];
}
public int length() {
return length;
}
public MyString concat(MyString str) {
int otherLength = str.length(); //Yanchevskaya A. Нет проверки на null
if (otherLength == 0) {
return this;
}
int length = this.chars.length + str.chars.length;
char[] temp = new char[length];
System.arraycopy(this.chars, 0, temp, 0, this.chars.length);
System.arraycopy(str.chars, 0, temp, this.chars.length, str.chars.length);
return new MyString(temp);
}
public int indexOf(int ch, int fromIndex) {
if (fromIndex < 0) return -1; //Yanchevskaya A. Можем объединить условия?
if (fromIndex >= length) return -1;
for (int i = fromIndex; i < length; i++)
if (chars[i] == ch)
return i;
return -1;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
MyString other;
if (!(o instanceof MyString)) return false;
else
other = (MyString) o;
if (this.chars.length != other.chars.length)
return false;
int i = 0;
while (i < this.chars.length) {
if (this.chars[i] != other.chars[i])
return false;
i++;
}
return true;
}
public MyString toLowerCase() {
char[] charCopy = new char[chars.length];
for (int i = 0; i < chars.length; i++) {
charCopy[i] = Character.toLowerCase(chars[i]);
}
return new MyString(charCopy);
}
public MyString toUpperCase() {
char[] charCopy = new char[chars.length];
for (int i = 0; i < chars.length; i++) {
charCopy[i] = Character.toUpperCase(chars[i]);
}
return new MyString(charCopy);
}
public MyString substring(int beginIndex, int endIndex) throws IndexOutOfBoundsException {
if (beginIndex < 0) {
throw new IndexOutOfBoundsException();
}
if (endIndex > chars.length) { //Yanchevskaya A.Можем объединить условия? Индекс также не может быть равен длине массива
throw new IndexOutOfBoundsException();
}
int sublength = (endIndex - beginIndex + 1);
char[] subchars = new char[sublength];
System.arraycopy(chars, beginIndex, subchars, 0, sublength);
return new MyString(subchars);
}
public MyString trim() {
int frontSpaces = 0, backSpaces = 0;
int i = 0;
//count spaces in front
while ((i < length) && (chars[i++] == ' '))
frontSpaces++;
i = length - 1;
//count spaces in back
while ((i >= frontSpaces) && (chars[i--] == ' '))
backSpaces++;
MyString result = new MyString();
if ((frontSpaces + backSpaces) <= length) {
result.length = this.length - frontSpaces - backSpaces;
result.chars = new char[result.length];
System.arraycopy(chars, frontSpaces, result.chars, 0, result.length);
}
return result;
}
public boolean contains(MyString string) { //Yanchevskaya A. Еще прошлое замечание – метод нужно реализовать
System.arraycopy(string.chars, 0, chars, 0, string.chars.length);
// for (int i = 0; i < string.chars.length; i++)
// chars[i] = string.chars[i];
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment