ll adlı nesnelerle bağlantılı bir listeye sahibim. Nesnem, adı, derecesi ve yaşı olan Öğrenci olarak adlandırılır. Öğrencinin adına göre bir bulma yöntemi oluşturmak istiyorum.Bağlantılı Listede yöntemi bul
import java.util.LinkedList;
import java.util.Scanner;
public class Course {
LinkedList<Object> ll = new LinkedList<Object>();
Scanner s = new Scanner(System.in);
public void addStudent() {
p("Enter the name that you want");
String f = s.nextLine();
Student a = new Student(f);
ll.add(a);
}
public void changeName() { //this method is to change the name of a student
Student student = findStudent();
p("Enter the name that you want");
String newName = s.nextLine();
Student.setName(newName);
}
public void setGrade() { //this method is to put the student's grade
Student student = findStudent();
p("Enter the grade that you want");
int grade = s.nextInt();
Student.setGrade(grade);
}
public void setAge() { //This method is to put the student's grade
Student student = findStudent();
p("Enter the age that you want");
int age = s.nextInt();
Student.setAge(age);
}
public Student findStudent(){
p("Which student do you want to change? Please enter their name:");
String name = s.nextLine();
boolean a=false;
for (int e=0; e<ll.size(); e++) {
if (name.equals(ll(e).name)) {
p("Found the student");
break;
return e;
//Find student in the list - left for the author
} else {
a=true;
}
}
if (a) {
p("Student is not in the list");
Student student = null;
return student;}
}
public String show() {
if (ll!=null) {
for (int i=0; i<ll.size(); i++) {
return "Student(name:" + name + ", age:" + age + ", grade:" + grade + ")";
}
} else {return "There are no students in the list";
}
}
//-------------------------------------------------------------------------
public void p(String x) {
System.out.println(x);
}
public static void main(String[] args) {
p("What do you want to do?");
Course course = new Course();
p("To add a student press 1");
p("To change the name of a student press 2");
p("To set the grade for a student press 3");
p("To set the age of a student press 4");
p("To find a student press 5");
p("To show the students press 6");
p("To get the age of a student press 7");
p("To get the grade of a student press 8");
int a = s.nextInt();
if (a==1) {
addStudent();
} else if (a==2) {
changeName();
}else if (a==3) {
setGrade();
}else if (a==4) {
setAge();
} else if (a==5) {
findStudent();
}else if (a==6) {
show();
} else if (a==7) {
getAge();
} else if (a==8) {
getGrade();
}
new Course();
}
}
Bulma yönteminde, (ll) sembolünü bulamadığı söylenir. Nesnemin bir parametresine dayalı bir bulma yöntemi yapmak için ne yapabilirim.
'if (name.equals (ll (e) .name)) {' -> ' if (name.equals (ll.get (e) .name)) {'** not alın. ** – markspace