2016-03-03 3 views
-1

Мне нужна помощь, чтобы переписать эту программу с помощью связанного списка. Любая помощь, которую вы можете мне дать, поможет. Я никогда раньше не использовал связанные списки, и до сих пор поиск в Интернете просто смутил меня. Вот мой код, который использует arraylist.Преобразование arraylist в связанный список в 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 
+0

Пожалуйста, задать конкретный вопрос здесь. В StackOverflow не рекомендуется ставить весь ваш код в вопрос и просить людей просто пойти на него. Прочтите справочный центр, если вы хотите узнать больше о том, что здесь уместно. –

+0

Просто замените везде, где вы видите 'ArrayList' с' LinkedList' – flakes

+0

Хорошо, извините, это одно из моих первых случаев использования этого сайта. Я посмотрю на справочный центр в следующий раз. – benderbot2004

ответ

1
LinkedList<Student> list = new LinkedList<Student>(); 
for(Student s: AllStudents) 
    list.add(s); 
Смежные вопросы