From scratch to learn JAVA (Experiment IX)

Zhejiang Wanli University lab report

Course: Object-Oriented Programming
Experiment name: Experimental nine Java collection classes
1, experimental purposes:
to master the typical use Java collection classes
II Experiment:

  1. LinkedList implemented using a simulation program KTV VOD system.
    Here Insert Picture Description
    2. Use Map to achieve access to the string "bwaerbctyxbacecrtdcvr"
    Here Insert Picture Description
    Third, the experimental procedures and results:
    Experiment 1:
    1) source:
    Import java.util.LinkedList;
    Import java.util.Scanner;
    public class txt2 {
    Private static void Show ( Song the LinkedList) {
    for (int I = 0; I <song.size (); I ++) {
    of System.out.print (I + ":");
    System.out.println (song.get (I) + "" );
    }
    }
    public static void main (String [] args) {
    System.out.println ( "A: to add a song list");
    System.out.println ( "B: the Top songs");
    the System.out. println ( "C: will move forward one song");
    System.out.println ( "D: exit");
    System.out.println ( "initial Album:");
    LinkedList song = new new LinkedList ();
    song.add ( "does not close the window - Steve Chou");
    song.add ( "Li Bai - Ronghao");
    song.add ( "Ann Bridge - Song Dong wild");
    song.add ( "When You Are Old - according to Zhao ");
    song.add (" prose poem written by his father - Li ");
    song.add (" once again re-meet - Sun Lu ");
    song.add (" boy - Liang Bo ");
    song.add ( "Chengdu - Lei Zhao");
    Show (Song);
    the while (to true) {
    of System.out.print ( "Please enter the number to be executed [AD]:");
    Scanner Scanner a new new = (the System.in);
    B = a.nextLine String ();
    Switch (B) {
    Case "a": {
    of System.out.print ( "Please enter the song to be added:");
    String A1 = new new Scanner (the System.in) .nextLine ( );
    song.add (a1);
    System.out.println ( "song has been added:" + a1);
    System.out.println ( "current song list:");
    Show (song);
    BREAK;
    }
    Case " B ": {
    System.out.print ( "Please enter the number you want to top song:");
    Scanner a2 = new new Scanner (System.in);
    String A22 = (String) song.get (a2.nextInt ());
    int the X-= song.indexOf (A22);
    IF (X <0) {
    System.out.println ( "you enter the song number does not exist!");
    } the else {
    song.remove (A22);
    song.addFirst (A22);
    System.out.println ( "current song list:");
    Show (song);
    }
    BREAK;
    }
    Case "C": {
    System.out.print ( "Please enter forward a number of songs:") ;
    Scanner Scanner new new A3 = (the System.in);
    String A33 = (String) song.get (a3.nextInt ());
    int Y = song.indexOf (A33);
    IF (Y <0) {
    the System.out. println ( "you enter the song number does not exist!");
    } {the else
    IF (Y == 0) {
    System.out.println ( "Enter the number of songs you have in the first place a!");
    } The else {
    song.remove (A33);
    song.add (the y-- 1, A33);
    System.out.println ( "The current song list:");
    Show (song);
    }
    }
    BREAK;
    }
    Case "D": {
    System.out.println ( "you've quit!");
    System.exit (1);
    BREAK;
    }
    }
    }
    }
    }
    2) result:
    Here Insert Picture Description
    experiment II:
    1) source:
    Import the java.util.Iterator;
    Import a java.util.Map;
    Import java.util.TreeMap;

public class txt1 {
public static void main(String[] args) {
String str = new String(“bwaerbctyxbacecrtdcvr”);
String s = getCharCount(str);
System.out.println(s);
}
public static String getCharCount(String str) {
char[] chs = str.toCharArray();
Map map = new TreeMap();
for (int i = 0; i < chs.length; i++) {
if (!(chs[i] >= ‘a’ && chs[i] <= ‘z’ || chs[i] >= ‘A’ && chs[i] <= ‘Z’))
continue;
Integer value = (Integer) map.get(chs[i]);
int count = 0;
if (value != null) {
count = value;
}
count++;
map.put(chs[i], count);
}
return mapToString(map);
}
private static String mapToString(Map<Character, Integer> map) {
StringBuilder sb = new StringBuilder();
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
Character key = (Character) it.next();
Integer value = map.get(key);
sb.append(key + “(” + value + “)”);
}
return sb.toString();
}
}
2)运行截图:

Here Insert Picture Description

Five experiments summarized:
 List (list): orderly, repeatable elements, you can insert multiple null elements. Common class implements the List interface has LinkedList, ArrayList, Vector, Stack.
 Set (set): disorder not repeat elements, up to a null element.
 Map (mapping): The collection sub-interface or not implemented class, Map is an interface. Map the most popular of several interfaces implementation class is HashMap, LinkedHashMap, Hashtable and TreeMap.

Published 16 original articles · won praise 0 · Views 540

Guess you like

Origin blog.csdn.net/want_to_fly_/article/details/104192428