简体   繁体   中英

How to display different data types of ArrayList<Object>?

Since I have different type of variables such Integer and string I think I need to use the ArrayList to store all those data. I can store all the data but I cannot display the data in specific type. For example, I let user input like code, songname, album...etc, and I want to display code, songname, album in separate line.

I don't know how to display each of the data in separate line but i know how to display it all. Can anyone help me with this?

package testmusic;

import java.util.ArrayList;
import java.util.Scanner;

public class MusicTest {

    // Arraylist
    ArrayList<Object> mylist=new ArrayList<Object>();

    // declare variables
    private Integer code;
    private String songname;
    private String singer;
    private String album;
    private String genre;
    private PriceTest pricetest; // aggregate with PriceTest (HAS-a)


    // default Constructor
    public MusicTest() {};


    // method for add music 
    void addMusicTest()
    {
        // Scanner
        Scanner input=new Scanner(System.in);

        // prompt user for how many song they want to input
        System.out.println("How Many Song You Want to Add Into the System?: ");
        int song=input.nextInt();

        // for loop
        for(int i=0;i<song;i++) 
        {
            System.out.print("Enter Code: ");
            mylistcode.add(input.nextInt());
            mylist.add(input.nextLine());
            System.out.print("Enter Song: ");
            mylist.add(input.nextLine());
            System.out.print("Enter Singer: ");
            mylist.add(input.nextLine());
            System.out.print("Enter Album: ");
            mylist.add(input.nextLine());
            System.out.print("Enter Genre: ");
            mylist.add(input.nextLine());
            System.out.print("Enter Cost Price: ");
            mylist.add(input.nextInt());
            System.out.print("Enter Sell Price: ");
            mylist.add(input.nextInt());
            System.out.println("DATA HAS BEEN CREATED!");

            input.close();

        }
    } // ends of addMusicTest


    // method for display
    void displayMusciTest() //MY PROBLEM IS IN HERE
    {
        for(Object str:mylist) 
        {
        System.out.println("Code: "+str);
        System.out.println("Title: "+str);
        System.out.println("Singer: "+str);  
        System.out.println("Album: "+str);
        System.out.println("Genre: "+str);  
        }
    }





    // GETTER METHOD
    public Integer getCode() {
        return code;
    }

    public String getSongname() {
        return songname;
    }
    public String getSinger() {
        return singer;
    }

    public String getAlbum() {
        return album;
    }

    public String getGenre() {
        return genre;
    }

    public PriceTest getPricetest() {
        return pricetest;
    }

    // SETTER METHOD    
    public void setCode(Integer code) {
        this.code = code;
    }

    public void setSongname(String songname) {
        this.songname = songname;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public void setPricetest(PriceTest pricetest) {
        this.pricetest = pricetest;
    }

}

You seem to have missed the point of your homework. You shouldn't be trying to store all of the individual items into a collection of Objects, you should have one class (perhaps Song ) with a collection of fields (like songname , singer etc).

You then have a collection of Song and you can use the same method to output each one (perhaps overriding toString() ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM