У меня возникли проблемы с написанием метода replace()
для класса, который я создаю, с именем LString
. Класс создает связанный объект списка для построения строк, аналогичный String
или StringBuilder
, но со связанными списками вместо массивов.написать метод replace() для связанного класса списка
replace(int start, int end, LString lStr)
это метод, который изменяет заданную LString
, связанный список, путем вставки другого LString
под названием lStr
между узлами start
и end
. Я изо всех сил пытаюсь представить себе эффективный способ написать его. Любые советы приветствуются, пытаясь выучить Java.
Вот мой код, replace()
находится вблизи дна:
import java.io.*;
import java.util.*;
public class LString {
node front;
int size;
//Creating a node class
private class node {
char data;
node next;
public node(){
}
public node (char newData){
this.data = newData;
}
public node (char newData, node newNext){
this.data = newData;
this.next = newNext;
}
}
//Constructors
public LString(){
this.size = 0;
this.front = null;
}
public LString(String original) {
this.size = original.length();
if (original.length() > 0){
this.front = new node(original.charAt(0));
node curr = this.front;
for (int i =1; i < original.length(); i++) {
curr.next = new node(original.charAt(i));
curr = curr.next;
}
}
}
// Length method, returns the length of LString
public int length() {
return this.size;
}
// compareTo method, compares this LString to anotherLString, returns 0 if equal,
// -1 if lexicogrpahically less, and 1 if lexicographically greater
public int compareTo(LString anotherLString) {
int len1 = length();
int len2 = anotherLString.length();
int lim = Math.min(len1, len2);
node cn1 = front;
node cn2 = anotherLString.front;
int k = 0;
while (k < lim) {
char c1 = cn1.data;
char c2 = cn2.data;
if (c1 != c2) {
return c1-c2;
}
k++;
cn1 = cn1.next;
cn2 = cn2.next;
}
return len1 - len2;
}
// a boolean equals method that returns true if LString and other are the same, false if not
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other instanceof LString) {
LString otherLString = (LString)other;
int n = length();
if (n == otherLString.length()) {
node n1 = front;
node n2 = otherLString.front;
while (n1 != null) {
if (n1.data != n2.data) {
return false;
}
n1 = n1.next;
n2 = n2.next;
}
return true;
}
}
return false;
}
// charAt returns the character of LString at the argument index
public char charAt(int index) {
if ((index < 0) || (index >= this.length())) {
throw new IndexOutOfBoundsException();
}
node curNode = front;
for (int i = 0; i < this.length(); i++, curNode = curNode.next) {
if (i == index) {
return curNode.data;
}
}
throw new IllegalStateException();
}
//
public void setCharAt(int index, char ch) {
if (index < 0 || index >= this.length()) {
throw new IndexOutOfBoundsException();
}
else {
node currNode = front;
for (int i = 0; i <this.length(); i++, currNode = currNode.next) {
if (i == index) {
currNode.data = ch;
}
}
}
}
public LString substring(int start, int end) {
if (start < 0 || end > this.length() || start > end) {
throw new IndexOutOfBoundsException();
}
LString substring = new LString();
if (start == end) {
return substring;
}
node node = this.front;
for (int i = 0; i < start; i++) {
node = node.next;
}
node copy = new node(node.data);
substring.front = copy;
for (int i = start+1; i < end; i++) {
node = node.next;
copy = copy.next = new node(node.data);
}
substring.size = end - start;
return substring;
}
public LString replace(int start, int end, LString lStr) {
if (start < 0 || end > this.length() || start > end) {
throw new IndexOutOfBoundsException();
}//??
}
public void append(char data){
this.size++;
if (front == null){
front = new node(data);
return;
}
node curr = front;
while (curr.next != null){
curr = curr.next;
}
curr.next = new node(data);
}
public void prepend (char data){
front = new node(data, front);
size++;
}
public void delete(int index){
if (index == 0){
front = front.next;
} else {
node curr = front;
for (int i = 0; i < index - 1; i++){
curr = curr.next;
}
curr.next = curr.next.next;
}
size--;
}
public String toString(){
StringBuilder result = new StringBuilder();
node curr = front;
while (curr != null){
result.append(curr.data);
curr = curr.next;
}
return result.toString();
}
public void add(int index, char data){
if (index == 0){
front = new node(data, front);
} else {
node curr = front;
for (int i = 0; i < index - 1; i++){
curr = curr.next;
}
curr.next = new node(data, curr.next);
}
}
}
'StringBuilder' уже существует. Зачем ты это делаешь? и [this] (http://stackoverflow.com/questions/28865738/substring-method-for-linked-list-object)? и [это] (http://stackoverflow.com/questions/28847451/writing-charat-and-setcharat-methods-for-linked-list-class)? и [это] (http://stackoverflow.com/questions/28844026/writing-an-equals-method-for-linked-list-object)? и [this] (http://stackoverflow.com/questions/28824474/lstring-class-using-linked-lists-to-make-strings-java)? И как долго это будет продолжаться? Каждый раз, когда вы добавляете метод? – EJP
Я обычный пользователь StackOverflow, который задал вам вопрос. Вы на это не ответили. – EJP