2014-10-29 76 views
-1

主類如何正確打印對象數組?

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

你可以使用

java.util.Arrays.toString(blah.split(delim)); 

對於這個工作,你CustomString類需要也提供了一個toString()方法。

0

您可以使用

System.out.println(new String(charArray)) 

Arrays.toString(charArray) 

到字符數組轉換成字符串打印的目的。 你也可以參考 What does the output of a printed character array mean?

而不是閱讀字符1 1並追加字符串。簡單的使用new String(data)

+0

當您更新類並覆蓋toString方法並且工作得很好時。那麼你的問題是什麼? – 2014-10-29 07:26:59

0

Unboxing可能有助於

嘗試

System.out.println("Splitted String: " + blah.split(delim).toString()); 
0

根據您的問題blah.split(DELIM)應該返回CustomString數據類型的數組。

我asuming你有一個叫CustomString類..

所以爲了從CustomString類型轉換爲你需要具體介紹一下CustomString正常字符串類型。

請註明類CustomString的內容,這樣我們就可以幫你

確定由CustomString類是存儲數據類型char類型的數組,因此外觀,但問題是分裂的返回類型( delim)是一個數據類型CustomString的數組,因此您將在使用tostring()進行拆箱時遇到問題。給我一分鐘

+0

我加了班的其餘部分 – leftlopez 2014-10-29 06:34:42