for the first time
I don't know how to write a blog for the first time. I hope you will understand.
At the end of a recent term, I wrote a pure JAVA KTV song ordering program (essentially the addition, deletion, modification and query of dynamic arrays)
A very simple project, no more nonsense, let's start.
program abstraction
I wrote two classes and an interface, which are: 1 Main 2. SongList 3. Ktvfunction (Interface)
(insert a sentence earlier, the KtvFunction interface is completely unnecessary to write. The purpose of writing is to facilitate the later review of which methods are used, which will be very helpful when the project volume is large)
code
Main
public class Main { public static void main(String[] args) { SongSheet songSheet = new SongSheet(); songSheet.Start();//Start method } }
KtvFunction
public interface KtvFunction { void Start(); //Add song void add(); //Top a song void top(); //Move one song forward void forwardOne(); //Delete a song void deleteSong(); //sign out void exit(); Method of traversing song list void SongListFor(); //Initialize the song list and add the default song content to the dynamic array void InitializeSongList(); }
SongList
import java.util.ArrayList; import java.util.Scanner; public class SongSheet implements KtvFunction{ final String MusicOne = "lasso pole"; final String MusicTwo = "The Hottest Ethnic Trend"; final String MusicThree = "Swing Song"; final String MusicFour = "Java Song of"; int whileNum = 0; //Create a dynamic array to store songs ArrayList<String> SongList = new ArrayList<>(); @Override public void Start(){ System.out.println("----------------" + "Changjiang 305 C Bedroom song ordering system" + "----------------"); //Initialization content InitializeSongList(); //Loop execution while (whileNum == 0){ //Print panel stuff printKtv(); //Cycle through the song list to see what songs there are SongListFor(); //Call input class library Scanner scanner = new Scanner(System.in); //Receive user entered numbers from the keyboard int Num = scanner.nextInt(); //Only when he enters the specified range value can he enter it if (Num <= 4 && Num >= 0) { switch (Num) { //Function button case 0: add(); break; case 1: top(); break; case 2: forwardOne(); break; case 3: deleteSong(); break; case 4: exit(); whileNum=1; break; } } else { System.out.println("Please re-enter the non range number you entered" + "\n"); } } } //Text content public void printKtv(){ System.out.println("0.Add songs to list"); System.out.println("1.Put the top of the song"); System.out.println("2.Move song forward one bit"); System.out.println("3.Delete a song"); System.out.println("4.sign out"); System.out.print("Current song list:"); } @Override public void InitializeSongList(){ SongList.add(MusicOne); SongList.add(MusicTwo); SongList.add(MusicThree); SongList.add(MusicFour); } //Method of traversing song list @Override public void SongListFor() { System.out.print("["); for (String s : SongList) { System.out.print(s + ","); } System.out.println("]"); } //Add song @Override public void add() { System.out.println("Please enter the name of the song you want to add"); //To be more precise, TODO should judge whether this song exists before adding it to the database Scanner scannerAdd = new Scanner(System.in); String newScannerAdd = scannerAdd.nextLine(); System.out.println("Songs added:" + newScannerAdd); System.out.print("Song list:" + "["); SongList.add(newScannerAdd); SongListFor(); System.out.print("\n"); } //Top a song @Override public void top() { int indexTop = 0; boolean existence;//Does it exist System.out.println("Please enter the song to top!"); Scanner scanner = new Scanner(System.in); String scannerTopSong = scanner.nextLine(); //Judge whether ScannerTopSong exists in the song list existence = SongList.contains(scannerTopSong); if (existence) { SongList.remove(scannerTopSong); SongList.add(indexTop, scannerTopSong); SongListFor(); System.out.print("\n"); }else{ System.out.println("Ghost! No,--" + scannerTopSong + "--This song ha!"); } } //Move song forward one bit @Override public void forwardOne() { boolean existence; System.out.println("Please enter the song name to move forward:"); Scanner scanner = new Scanner(System.in); String position = scanner.nextLine(); if (SongList.get(0).equals(position)) {//If it's the first one, you don't need to replace it. Just output it directly System.out.println("eldest brother!!! It's the first. What's ahead of time?"); SongListFor(); System.out.println("\n"); }else { //Query whether there is this value in the dynamic array existence = SongList.contains(position); //if else: used to judge what does exist and what does not exist if(existence){ for (int i = 0; i < SongList.size(); i++) { if (SongList.get(i).equals(position)) {//Find the target song and replace the position String blank = SongList.get(i-1); SongList.set((i-1),SongList.get(i)); SongList.set((i),blank); break; } } SongListFor();//Reprint it for him System.out.print("\n"); }else { System.out.println("Ghost! No,--" + position + "--This song ha!"); } } } //Delete song @Override public void deleteSong(){ String deleteSong = null; boolean existence; System.out.println("Please enter the song name to move forward:"); Scanner scanner = new Scanner(System.in); String scannerMusicName = scanner.nextLine(); for (int i = 0; i < SongList.size(); i++) { if (SongList.get(i).equals(scannerMusicName)){ deleteSong = scannerMusicName; SongList.remove(i);//Delete target song System.out.println("The target song list has been deleted for you" + "\n"); } } if(deleteSong == null){ System.out.println("There are no songs you entered in the song list:--" + scannerMusicName + "--song" + "\n"); } } //Exit song list @Override public void exit() { System.out.println("You have exited Changjiang 305 C Bedroom song ordering system"); } }
analysis
1. From the beginning, I thought of using arrays, but the problem is: the songs in the song list are dynamically increased and decreased. It is not allowed to use conventional arrays. Because the length of the array is already known when it is used, I can't use arrays, but should use dynamic arrays instead.
2. Secondly, why is the function of modifying songs not set? Generally speaking, songs cannot be modified, because once you modify the song name of a song, it means that the content of its lyrics is different. By adding the function of songs, you can achieve the effect of ordering songs, and there is no need to modify songs.
3. The naming of many places in the text is not unified, and it is easy to see the word is unclear. Secondly, they are all used inside the method, using local variables, so they will not have a great impact
summary
This program is essentially the addition, deletion, modification and query of dynamic array. I hope to see that this program can deepen the understanding of dynamic array
In addition, I put the Chinese document of ArrayList here. Interested students can go and have a look. All dynamic arrays are put in it
ArrayList - Java 11 Chinese version - API reference document (apiref.com)