import java.util.ArrayList;
public class Student
{
private String name;
private int row;
private int column;
private ArrayList<Student> friends = new ArrayList<Student>;
public Student(String newName, int newRow, int newColumn)
{
name = newName;
row = newRow;
column = newColumn;
}
public Student(String newName)
{
name = newName;
}
public String getName()
{
return name;
}
public int getRow()
{
return row;
}
public int getColumn()
{
return column;
}
public void setRow(int newRow)
{
row = newRow;
}
public void setColumn(int newColumn)
{
column = newColumn;
}
public void addFriend(Student friend)
{
friends.add(friend);
}
public double getUnhappiness()
{
double unhappiness;
unhappiness = Math.sqrt(((this.getRow() - friends.getRow()) * (this.getRow() - friends.getRow())) -
((this.getColumn() - friends.getColumn()) * (this.getColumn() - friends.getColumn())));
return unhappiness;
}
}
对于getUnhappiness方法,我想知道如何访问我作为实例变量的arraylist中的Students的行和列。例如,我试图获取学生的行和列,以便我可以找到不快乐
答案 0 :(得分:0)
friends.getRow()
和friends.getColumn()
会给你错误,因为这些方法在arraylist中不存在。
我想知道如何访问arraylist中学生的行和列
您必须在arraylist中指定元素,例如:
friends.get(0).getRow() //get row of friends' first element
friends.get(4).getRow() //get row of friends' fifth element