2017-05-19 258 views
1

我有點麻煩,搞清楚如何適當地使一個對象不僅包含字符串和整數,而且還包含字符串數組和int數組。目前,我有這個如何在Java中創建一個包含字符串,字符串數組和int數組的對象?

public class Gamebook { 
    Section[] sections = new Section[6]; 
    String storyText; 
    String[] choiceText; 
    int[] choiceSections; 

public Gamebook() { 
    storyText = ""; 
    choiceText = new String[1]; 
    choiceSections = int[];  
    } 
} 

如果我理解正確的話,這將創建節的對象,那些6陣列,以及允許我的部分對象有一個storyText串,choiceText字符串數組和choiceSections int數組。

但是,這似乎是硬編碼,我需要能夠通過我有一個txt文件,讀出故事文本和其他必要的信息,並將這些片斷分配給每個部分對象。我發現很少或沒有成功,並覺得我可能缺乏對Java的對象方向的理解,但在看過YouTube上的幾個視頻後,他們大多數沒有處理具有多個屬性的對象,而是那些手動設置它們的對象使用方法解析信息以填充「空白」。

+0

您可以發佈您段聲明 –

+0

這是第(字符串,字符串[],INT []) – Serkuto

+0

我推薦使用ArrayList,但我仍然想知道爲什麼你使用課堂部分?這個類包含三個屬性,那麼問題是什麼 –

回答

1

這裏是你可以打一個比方。這使用列表而不是數組。您可以將列表替換爲數組,但必須在構造函數中爲數組設置大小。此外,添加陣列會產生新的問題,您將不得不處理。

AlbumInfo類:

import java.util.*; 

/** 
* 
* @author Sedrick 
*/ 
public class AlbumInfo { 

    private String albumName; 
    private String artist; 
    private List<String> tracksTitle; 
    private List<String> tracksLength; 

    public AlbumInfo() 
    { 
     albumName = "Add Album Name"; 
     artist = "Add Artist Name"; 
     tracksTitle = new ArrayList(); 
     tracksLength = new ArrayList(); 
    } 

    /** 
    * @return the albumName 
    */ 
    public String getAlbumName() 
    { 
     return albumName; 
    } 

    /** 
    * @param albumName the albumName to set 
    */ 
    public void setAlbumName(String albumName) 
    { 
     this.albumName = albumName; 
    } 

    /** 
    * @return the artist 
    */ 
    public String getArtist() 
    { 
     return artist; 
    } 

    /** 
    * @param artist the artist to set 
    */ 
    public void setArtist(String artist) 
    { 
     this.artist = artist; 
    } 

    /** 
    * @return the tracksTitle 
    */ 
    public List<String> getTracksTitle() 
    { 
     return tracksTitle; 
    } 

    /** 
    * @param tracksTitle the tracksTitle to set 
    */ 
    public void addTrackTitle(String trackTitle) 
    { 
     this.tracksTitle.add(trackTitle); 
    } 

    /** 
    * @return the tracksLength 
    */ 
    public List<String> getTracksLength() 
    { 
     return tracksLength; 
    } 

    /** 
    * @param tracksLength the tracksLength to set 
    */ 
    public void addTrackLength(String trackLength) 
    { 
     this.tracksLength.add(trackLength); 
    } 

} 

主要測試類:

import java.util.*; 

/** 
* 
* @author Sedrick 
*/ 
public class AlbumTest { 

    static final String[] trackTitles = {"Ambitionz Az a Ridah", "All Bout U", "Skandalouz", "Got My Mind Made Up", "How Do U Want It", "2 of Amerikaz Most Wanted", "No More Pain", "Heartz of Men", "Life Goes On", "Only God Can Judge Me", "Tradin' War Stories", "California Love(Remix)", "I Ain't Mad at Cha", "What'z Ya Phone #"}; 

    static final String[] trackLength = {"4:39", "4:37", "4:09", "5:14", "4:47", "4:07", "6:14", "4:43", "5:02", "4:57", "5:29", "6:25", "4:53", "5:10"}; 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     // Add album info 
     AlbumInfo allEyesOnMeDiscOne = new AlbumInfo(); 
     allEyesOnMeDiscOne.setAlbumName("All Eyes On Me"); 
     allEyesOnMeDiscOne.setArtist("Tupac"); 
     for (int i = 0; i < trackTitles.length; i++) { 
      allEyesOnMeDiscOne.addTrackTitle(trackTitles[i]); 
     } 

     for (String entry : trackLength) { 
      allEyesOnMeDiscOne.addTrackLength(entry); 
     } 

     //Print album info 
     System.out.println("Album Name: " + allEyesOnMeDiscOne.getAlbumName()); 
     System.out.println("Album Artist: " + allEyesOnMeDiscOne.getArtist()); 
     List albumTitles = allEyesOnMeDiscOne.getTracksTitle(); 
     List albumTitlesLength = allEyesOnMeDiscOne.getTracksLength(); 

     for (int i = 0; i < albumTitles.size(); i++) { 
      System.out.println("Title: " + albumTitles.get(i) + " Length: " + albumTitlesLength.get(i)); 
     } 

    } 
} 
+0

這正是我尋找的故障!非常感謝!我是一名極其視覺的學習者,渴望在我的腰帶上獲得更多的編碼,剛開始一週前,進展順利,但這對每噸都會有幫助! – Serkuto

1

數組的問題是它們的大小是固定的。含義:你需要知道的元素數寫

someArray = new Whatever[... 

這意味着當:從文件中解析信息時,你有兩個選擇:

  • 開始用大空數組,並跟蹤的您將這些元素放入這些空白插槽中。空間不足時,您可能必須將東西複製到較大的陣列中。您最後可以將條目複製到尺寸完美的陣列中。
  • 你轉向Java列表和ArrayList - 它允許動態添加和刪除元素。
+0

感謝關於數組部分的解釋,但我更加努力將所有這些屬性添加到單個對象的概念。 – Serkuto

+0

我沒有得到你的完整問題。也許這將有助於將部分類的相關部分添加到您的問題中。無論如何,屬於某個部分的所有東西都應該是部分類中的字段。在另一個類中不應該有另一個數組,其細節屬於該部分。 – GhostCat

2

使用Araylist它將存儲您的所有對象看看這個tutorial它會幫助你或本one

相關問題