在 Java 中将数组列表转换为链表



我需要帮助尝试使用链表重写此程序。你能给我的任何帮助都会有所帮助。我以前从未使用过链表,到目前为止,在线查找只是让我感到困惑。这是我使用数组列表的代码。

public class Main {
public static void main(String[] args) throws Exception {
    String stdID, sName, fName, lName, mName;
    int testScore1, testScore2, testScore3,totalNoHours;
    float cGPA;
    Students workobj;
    //****************************************External Output*****************************************************
    PrintWriter output;
    output = new PrintWriter(new File("StudentRecords.txt"));
    //****************************************External Input******************************************************
    try {
        //opening the file for input
        FileInputStream istream = new FileInputStream("input.txt");
        Scanner input = new Scanner(istream);
        //creating an arraylist to store student objects
        ArrayList<Students> AllStudents = new ArrayList<Students>();
        while (input.hasNextLine()) {
            //first I will read the student id
            stdID = input.next();
            //next I will read the student name
            fName = input.next();
            lName = input.next();
            if (input.hasNextInt()) {
                mName = "";
            } else {
                mName = input.next();
            }
            sName = fName + " " + lName + " " + mName;
            //next read in the test scores
            testScore1 = input.nextInt();
            testScore2 = input.nextInt();
            testScore3 = input.nextInt();
            //next read in totalNoHours
            totalNoHours = input.nextInt();
            //next read in cGPA
            cGPA = input.nextFloat();
            //creating a student object
            Students StudentRecord = new Students(stdID, sName, testScore1, testScore2, testScore3,totalNoHours,cGPA);
            //now store this in allstudents
            AllStudents.add(StudentRecord);
        }//end of while
}//end of main
}//end of program
LinkedList<Student> list = new LinkedList<Student>();
for(Student s: AllStudents)
    list.add(s);
    public static void main(String[] args) 
    {
     List<Integer> t=new ArrayList<Integer>();
     t.add(1);
     t.add(2);
     t.add(5);
     t.add(3);
     System.out.println("ArrayList upcating to list");
     for(Integer x:t)
     {
         System.out.println(x);
     }
     System.out.println(t instanceof ArrayList);
     System.out.println(t instanceof LinkedList);
     t=new LinkedList<Integer>(t);
     System.out.println("After upcasted to linkedList");
     for(Integer x:t)
     {
        System.out.println(x);
     }
     System.out.println(t instanceof LinkedList);
     System.out.println(t instanceof ArrayList);
    }

相关内容

  • 没有找到相关文章

最新更新