如何在Java中创建一个包含字符串,字符串数组和int数组的对象?

时间:2017-05-19 03:25:58

标签: java arrays string object

我在弄清楚如何正确地创建一个不仅包含字符串和整数的对象,而且还包含字符串数组和int数组时遇到了一些麻烦。我目前有这个

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

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

如果我理解正确,这将创建一个section对象数组,其中6个,以及允许我的section对象有一个storyText字符串,一个choiceText字符串数组和一个choiceSections int数组。

然而,这似乎是硬编码的,我需要能够读取我拥有的txt文件,提取故事文本和其他必要信息,并将这些部分分配给每个部分对象。我发现很少甚至没有成功,觉得我可能对Java的面向对象缺乏了解,但是在youtube上观看几个视频之后,他们中的大多数都没有处理具有多个属性的对象,而那些手动设置它们的对象他们自己而不是使用方法来解析信息以填补"空白"。

3 个答案:

答案 0 :(得分:2)

使用 Araylist 它会存储您的所有对象,请查看此tutorial它会对您有帮助one

答案 1 :(得分:1)

数组的问题是它们的大小是固定的。含义:编写

时需要知道元素的数量
someArray = new Whatever[... 

这意味着:在解析文件中的信息时,您有两个选择:

  • 从一个大的空数组开始,并跟踪放入这些空插槽的元素。当空间不足时,您可能必须将事物复制到更大的数组中。您可以在最后以完美大小的数组复制条目。
  • 转向Java List和ArrayList - 允许动态添加和删除元素。

答案 2 :(得分:1)

以下是您可以使用的示例。这使用Lists而不是Arrays。您可以使用数组替换列表,但必须在构造函数中为数组设置大小。此外,添加数组将产生您必须处理的新问题。

  

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));
        }

    }
}