主要班级
public CustomString[] split(char delimiter) {
// char n;
char[] split = null;
int numOfSplit = 1;
int objectLocation=0;
for (int h = 0; h < this.data.length; h++) {
if (delimiter == this.data[h]) {
numOfSplit++;
}
}
CustomString[] splitter = new CustomString[numOfSplit];
for (int i = 0; i < this.data.length; i++) {
if (delimiter == this.data[i]) {
// n = 32;//unicode for space}
split = new char[i];
for (int j = 0; j < split.length; j++) {
char z = this.data[j];
split[j] = z;
objectLocation++;
}
}
splitter[objectLocation] = new CustomString(split);
}
return splitter;
}
测试员班:
System.out.print("Delimiter: ");
String delimiter = input.next();
char[] deli = delimiter.toCharArray();
char delim = deli[0];
System.out.println("Splitted String: " + blah.split(delim));
结果:[LCustomString; @ 55f96302
主要课程已完成
import java.util.Scanner;
import java.util.Arrays;
public class CustomString {
private char[] data;
public CustomString() {
} // no arg constructor
public CustomString(char[] data) {
this.data = data;
} // constructor
public CustomString changeCase() { // type: CustomString
char n;
char[] newArray = new char[this.data.length];// this.data;
// char charNewA
for (int i = 0; i < newArray.length; i++) {
n = this.data[i];
// char[] number = new char[1];
if (n >= 65 && n <= 90) {
n += 32;
}// if capital char change to lower char
else if (n >= 97 && n <= 122) {
n -= 32;
}// if lower char change to capital char
// create a new array with n properties
newArray[i] = n;
}
return new CustomString(newArray);
} // returns a new CustomString where the case of each letter is changed to
// the opposite
public char charAt(int index) {
char charIndex = this.data[index - 1];
return charIndex;
} // returns character as a given index
public int compareTo(CustomString rhs) { // take off comments
int value = 0;
int loopSize;
if (this.data.length < rhs.data.length) {
loopSize = this.data.length;
} else {
loopSize = rhs.data.length;
}
for (int i = 0; i < loopSize; i++) {
char char1 = this.data[i];
char char2 = rhs.data[i]; // should be equal to 2nd customString
if (char1 != char2) {
value = char1 - char2;
// if (char1 > char2) {
// value = char1-char2; //pos??
// } else if (char1 < char2) {
// value =
// }
}
}
return value;
} // compares 2 strings lexographically. return 0 if two same. return + is
public int compareToIgnoreCase(CustomString rhs) {
char[] newArrayCompareToIgnoreCase = new char[this.data.length];
// newArrayCompareToIgnoreCase = this.data;
char[] newArrayCompareToIgnoreCase2 = new char[rhs.data.length];
// newArrayCompareToIgnoreCase2 = rhs.data;
char n, m;
int value = 0;
int loopSize;
if (this.data.length < rhs.data.length) {
loopSize = this.data.length;
} else {
loopSize = rhs.data.length;
}
for (int i = 0; i < loopSize; i++) {
n = this.data[i];
// char[] number = new char[1];
if (n >= 97 && n <= 122) {
n -= 32;
}// if lower char change to capital char
newArrayCompareToIgnoreCase[i] = n;
m = rhs.data[i];
if (m >= 97 && m <= 122) {
m -= 32;
}// if lower char change to capital char
newArrayCompareToIgnoreCase2[i] = m;
// by now everything should be lowercase
for (int j = 0; j < loopSize; j++) {
char char1 = newArrayCompareToIgnoreCase[j];
char char2 = newArrayCompareToIgnoreCase2[j];
if (char1 == char2) {
value = 0;
}
if (char1 != char2) {
value = char1 - char2;
}
}
}
return value;
} // compares two string but casing douse not matter
public CustomString concat(CustomString rhs) {
char n, m;
char[] newArrayConcat = new char[this.data.length + rhs.data.length + 2];
for (int i = 0; i < this.data.length; i++) {
n = this.data[i];
// m = rhs.data[i];
newArrayConcat[i] = n;
// newArrayConcat[i+this.data.length]= m;
}
for (int j = 0; j < rhs.data.length; j++) {
m = rhs.data[j];
newArrayConcat[j + this.data.length] = m; // +1
}
return new CustomString(newArrayConcat); // change?
}// returns a new CustomString object by concatenating source string and
// parameter string
// CustomString
public boolean equals(CustomString rhs) {
char[] newArrayEquals = new char[this.data.length];
char[] newArrayEquals2 = new char[rhs.data.length];
boolean equals = false;
int length;
char n, m;
if (this.data.length > rhs.data.length) {
length = rhs.data.length;
} else {
length = this.data.length;
}
for (int i = 0; i < length; i++) {
n = this.data[i];
m = rhs.data[i];
newArrayEquals[i] = n;
newArrayEquals2[i] = m;
}
for (int j = 0; j < length; j++) {
char char1 = newArrayEquals[j];
char char2 = newArrayEquals2[j];
if (char1 != char2) {
equals = false;
break;
} else if (char1 == char2) {
equals = true;
}
}
return equals;
} // Returns true or false based on whether or not the two strings are
// equal. NOTE: Does not ignore case
public boolean equalsIgnoreCase(CustomString rhs) {
int length;
if (this.data.length > rhs.data.length) {
length = rhs.data.length;
} else {
length = this.data.length;
}
char[] newArrayEqualsToIgnoreCase = new char[this.data.length];
char[] newArrayEqualsToIgnoreCase2 = new char[rhs.data.length];
char n, m;
boolean equalsIgnoreCase = false;
for (int i = 0; i < length; i++) { // must compare which string is
// longer or else if
// //newArrayEqualsToIgnoreCase.length
n = this.data[i];
m = rhs.data[i];
if (n >= 65 && n <= 90) {
n += 32;
}
if (m >= 65 && m <= 90) {
m += 32;
}// changes everything to lower case
newArrayEqualsToIgnoreCase[i] = n;
newArrayEqualsToIgnoreCase2[i] = m;
}
for (int j = 0; j < length; j++) { // this.data.length
char char1 = newArrayEqualsToIgnoreCase[j];
char char2 = newArrayEqualsToIgnoreCase2[j];
if (char1 != char2) {
equalsIgnoreCase = false;
break;
} else if (char1 == char2) {
equalsIgnoreCase = true;
}
}
return equalsIgnoreCase; // change?
} // Same as equals but ignores the case
public int length() {
int charLength = this.data.length;
return charLength; // change?
} // Returns the length of the CustomString object
public CustomString[] split(char delimiter) {
// char n;
char[] split = null;
int numOfSplit = 1;
int objectLocation=0;
for (int h = 0; h < this.data.length; h++) {
if (delimiter == this.data[h]) {
numOfSplit++;
}
}
CustomString[] splitter = new CustomString[numOfSplit];
for (int i = 0; i < this.data.length; i++) {
if (delimiter == this.data[i]) {
// n = 32;//unicode for space}
split = new char[i];
for (int j = 0; j < split.length; j++) {
char z = this.data[j];
split[j] = z;
objectLocation++;
}
}
splitter[objectLocation] = new CustomString(split);
}
return splitter;
} // Returns a CustomString array, where each element of the array is a
// CustomString object created by splitting the source string based
// on the given char delimiter. This is an easier version of the split
// method provided in the String class and
// you only need to split on one character. The output array should NOT
// contain the delimiter character.
public boolean startsWith(CustomString prefix) {
boolean startsWithEqual = false;
char[] startsWith1 = new char[prefix.data.length];
char[] startsWith2 = new char[prefix.data.length];
char m, n;
for (int i = 0; i < prefix.data.length; i++) {
m = this.data[i];
n = prefix.data[i];
startsWith1[i] = m;
startsWith2[i] = n;
}
for (int j = 0; j < prefix.data.length; j++) {
if (startsWith1[j] == startsWith2[j]) {
startsWithEqual = true;
}
if (startsWith1[j] != startsWith2[j]) {
startsWithEqual = false;
break;
}
}
return startsWithEqual;
} // Returns true if the source CustomString starts with the given prefix
public boolean endsWith(CustomString suffix) {
boolean endsWithEqual = false;
char[] endsWith1 = new char[suffix.data.length];
char[] endsWith2 = new char[suffix.data.length];
char m, n;
for (int i = 0; i < suffix.data.length; i++) {
n = suffix.data[i];
endsWith2[i] = n;
}
int k = 0;
for (int i = this.data.length - suffix.data.length; i < this.data.length; i++) {
m = this.data[i];
endsWith1[k] = m;
k++;
}
for (int j = 0; j < suffix.data.length; j++) {
if (endsWith1[j] == endsWith2[j]) {
endsWithEqual = true;
}
if (endsWith1[j] != endsWith2[j]) {
endsWithEqual = false;
break;
}
}
return endsWithEqual;
}
// Returns true if the source CustomString contains the parameter
public CustomString substring(int srcBegin) {
char[] newArraySub1;
if (srcBegin == 0) {
newArraySub1 = new char[this.data.length - srcBegin];
} else {
newArraySub1 = new char[this.data.length - srcBegin + 1];
}
char n;
for (int i = srcBegin; i < newArraySub1.length; i++) {
n = this.data[i];// -1
newArraySub1[i] = n;
}
return new CustomString(newArraySub1);
} // Returns a new CustomString object created by finding the substring of
// the source string starting
// with src Begin and going to the end of the source string
public CustomString substring(int srcBegin, int srcEnd) {
char n;
char[] newArraySub2 = new char[this.data.length - srcBegin];
for (int i = srcBegin; i < srcEnd; i++) {
n = this.data[i];
newArraySub2[i] = n;
}
return new CustomString(newArraySub2);
}// Returns a new CustomString object created by finding the substring of
// the source starting with srcBegin and ending at srcEnd –
public CustomString titleize() {
char n, m;
char[] titleizedChar = new char[this.data.length];
for (int j = 0; j < this.data.length; j++) {
m = this.data[j];
if (m >= 65 && m <= 90) {
m += 32;
titleizedChar[j] = m;
}
for (int i = 0; i < 1; i++) {
n = this.data[i];
if (n >= 97 && n <= 122) {
n -= 32;
}
titleizedChar[i] = n;
}
titleizedChar[j] = m;
}
return new CustomString(titleizedChar); // change?
} // Returns a new CustomString object where the first character or every
// word is capitalized.
public CustomString toLowerCase() {
char[] toLowCase = new char[this.data.length];
for (int i = 0; i < toLowCase.length; i++) {
char n = this.data[i];
if (n >= 65 && n <= 90) {
n += 32;
}
toLowCase[i] = n;
}
return new CustomString(toLowCase);
} // Returns a new CustomString object with all lower case letters.
public CustomString toUpperCase() { // should be similar to lower case do
char[] toUpperCase = new char[this.data.length];
for (int i = 0; i < toUpperCase.length; i++) {
char n = this.data[i];
if (n >= 97 && n <= 122) {
n -= 32;
}
toUpperCase[i] = n;
}
return new CustomString(toUpperCase);
} // Returns a new CustomString object with all upper case letters.
public String toString() {
String toString1 = "";
for (int i = 0; i < this.data.length; i++) {
char n = this.data[i];
toString1 += n;
}
return toString1;
} // Returns a String representation of the CustomString object. This is the
// only place where you are allowed to use a String variable to build
// the output string and return it.
}// end of code
我试图在阵列中打印每个对象,但不知道怎么做?注意:我已经有了toString方法。
编辑:我在晚上10:45更改了原始帖子中的主要课程。我发布了错误的一个
答案 0 :(得分:0)
您可以使用
java.util.Arrays.toString(blah.split(delim));
为此,您的CustomString类也需要提供toString()
方法。
答案 1 :(得分:0)
您可以使用
System.out.println(new String(charArray))
或
Arrays.toString(charArray)
将字符数组转换为字符串以进行打印。 你也可以参考 What does the output of a printed character array mean?
而不是逐个读取char 1并附加在字符串中。简单使用new String(data)
答案 2 :(得分:0)
Unboxing
可能会有帮助
尝试
System.out.println("Splitted String: " + blah.split(delim).toString());
答案 3 :(得分:0)
根据你的问题,blah.split(delim)应该返回一个CustomString数据类型的数组。
我觉得你有一个名为CustomString的类..
因此,为了将From CustomString类型转换为普通字符串类型,您需要具体说明CustomString。
请提及Class CustomString的内容,以便我们帮助您
好吧,看看CustomString类它存储数据类型char的数组,但问题是split(delim)的返回类型是数据类型CustomString的数组,因此在使用tostring()取消装箱时会遇到问题。给我一分钟