We're suppose to do tutorial before class but im having trouble with one questions. I have to complete this code:
public class Tester2 {
public static void main(String[] args) {
Employee[] list = new Employee[5];
Employee emp1 = new Employee("Harvey", 75000.00);
Employee emp2 = new Employee("Donna", 10000.00);
Employee emp3 = new Employee("Mike", 40000.00);
addElement(emp1, list);
addElement(emp2, list);
addElement(emp3, list);
insertElementAt(new Employee("Jessica", 100000.00), list, 1);
displayElements(list); // Line 1
removeElementAt(list, 0);
System.out.println("");
displayElements(list); // Line 2
}
public static void displayElements(Employee[] list) {
for (Employee element: list) {
if (element != null) {
System.out.println(element.getName() + " RM " +
element.getSalary());
}
}
}
public static void addElement(Employee emp, Employee[] list) {
for (int i=0; i<list.length; i++) {
if (list[i] == null) {
list[i] = emp;
break;
}
}
}
public static void insertElementAt(Employee emp, Employee[] list,
int pos) {
// Complete the body!
}
public static void removeElementAt(Employee[] list, int pos) {
// Complete the body!
}
}
im not sure what im suppose to put at void insertElementAt. i thought this:
`emp.setname("Jessica");
emp.setEmpCode(100000.00);
list.add(emp);`
but then i saw this line :insertElementAt(new Employee("Jessica", 100000.00), list, 1); and i think theres a different way but i dont know. please help.
You can achieve it by incrementing and decrementing the array based on position of insert and remove in a new array and then copy the array.
Below is the code which worked
public static void insertElementAt(Employee emp, Employee[] list,
int pos) {
Employee[] nlist = new Employee[5];
for (int i=0; i<list.length; i++) {
if(i==pos){
nlist[i]=emp;
}else if(i>pos){
nlist[i]=list[i-1];
}else{
nlist[i]=list[i];
}
}
System.arraycopy(nlist, 0, list, 0, 5);
}
public static void removeElementAt(Employee[] list, int pos) {
Employee[] nlist = new Employee[5];
for (int i=0; i<list.length; i++) {
if(i>=pos){
if(i<4){
nlist[i]=list[i+1];;
}
}else{
nlist[i]=list[i];
}
}
System.arraycopy(nlist, 0, list, 0, 5);
}
Related
I haven't coded in over a year and I'm really struggling to remember anything. I have 100 tabs open trying to relearn but I'm truly stuck.
This is my main method.
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
StudentDatabase database = new StudentDatabase();
database.addStudent("Joe Schmo", 1101, 3.2);
database.addStudent("Katie Katerson", 31415926, 3.7);
database.addStudent("Watson TheBassetHound", 12345, 2.4);
database.printDatabase();
StudentDatabaseLL databaseLL = new StudentDatabaseLL();
databaseLL.addStudent("Joe Schmo", 1101, 3.2);
databaseLL.addStudent("Katie Katerson", 31415926, 3.7);
databaseLL.addStudent("Watson TheBassetHound", 12345, 2.4);
databaseLL.printDatabase();
}
}
In another code file I need to print all the information of a student at a certain index. I literally cannot figure it out.
public class StudentDatabase {
private Student[] database;
public StudentDatabase() {
database = new Student[0];
}
public void addStudent(String name, int idNumber, double gpa) {
Student student = new Student(name, idNumber, gpa);
Student[] newDatabase = new Student[database.length + 1];
for (int i = 0; i < database.length; i++) {
newDatabase[i] = database[i];
}
newDatabase[newDatabase.length - 1] = student;
database = newDatabase;
}
public void printDatabase() {
for (int i = 0; i < database.length; i++) {
Student student = database[i];
System.out.println(student.getName() + ": (ID=" + student.getIdNumber() + "), (gpa=" + student.getGPA() + ")");
System.out.println();
}
}
public class findStudentbyIndex {
public void findStudentbyIndex(String[] args){
int[] intArray = StudentDatabase
)
}
}
This is all I have. Could someone just point me in the right direction?
Here is how you should write the function and you should declare this func in the StudentDatabase class, as it is a function of it (according to OOP)
public void findStudentbyIndex(int i)
{
Student std = database[i];
System.out.println(std.getName() + ": (ID=" + std.getIdNumber() + "), (gpa=" + std.getGpa() + ")");
}
Sorry for the edit, i'm kinda new to this...
Your code seems a little bit complex for the expected result. You should not have such class like findStudentbyIndex.. the method should be part of StudentDatabase.
Case 1: I will propose a solution using your implementation
You should remove then your class findStudentbyIndex. Put everything in student database.
public class StudentDatabase {
private Student[] database;
public StudentDatabase() {
database = new Student[0];
}
public void addStudent(String name, int idNumber, double gpa) {
Student student = new Student(idNumber, name, gpa);
Student[] newDatabase = new Student[database.length + 1];
for (int i = 0; i < database.length; i++) {
newDatabase[i] = database[i];
}
newDatabase[newDatabase.length - 1] = student;
database = newDatabase;
}
public void printDatabase() {
for (int i = 0; i < database.length; i++) {
Student student = database[i];
System.out.println(student.getName() + ": (ID=" + student.getIdNumber() + "), (gpa=" + student.getGpa() + ")");
}
}
public void getStudentById(int idNumber) {
for (int i = 0; i < database.length; i++) {
if(database[i].getIdNumber() == idNumber) {
System.out.println(database[i]);
}
}
}
// other version:
public void getStudentById2(int idNumber) {
for (Student student: database) {
if(student.getIdNumber() == idNumber) {
System.out.println(student);
}
}
}
// modern way
public void getStudentById3(int idNumber) {
System.out.println(
Arrays.stream(database)
.filter(student -> student.getIdNumber() == idNumber)
.findFirst().get()
);
}
}
Here is how you'd call it in your main :
public static void main(String[] args) {
StudentDatabase database = new StudentDatabase();
database.addStudent("Joe Schmo", 1101, 3.2);
database.addStudent("Katie Katerson", 31415926, 3.7);
database.addStudent("Watson TheBassetHound", 12345, 2.4);
database.printDatabase();
database.getStudentById(31415926);
database.getStudentById2(31415926);
database.getStudentById3(31415926);
}
Case 2: why don't you use java features ? You have lists, Maps...
In your code you have StudentDatabase and StudentDatabaseLL objects ... I do suppose they look the same.
Here is what I would do for your case:
public class Main {
private static Map<String, List<Student>> studentListsMap = new HashMap<>();
public static void main(String[] args) {
List<Student> students;
students = new ArrayList<>();
students.add(new Student(11, "name1", 3.2));
students.add(new Student(12, "name2", 4.335));
students.add(new Student(13, "name3", 12.1));
// will print all students
printDatabase(students);
System.out.println("*******");
getStudentById(students, 11);
studentListsMap.put("students", students);
students = new ArrayList<>();
students.add(new Student(11, "name1", 3.2));
students.add(new Student(12, "name2", 4.335));
students.add(new Student(13, "name3", 12.1));
// will print all students
printDatabase(students);
System.out.println("*******");
getStudentById(students, 12);
getStudentById(students, 15);
studentListsMap.put("studentsLL", students);
// now you can also manage your two lists in the map
System.out.println("from the map");
printDatabase(studentListsMap.get("studentsLL"));
System.out.println("*******");
getStudentById(studentListsMap.get("studentsLL"), 13);
}
private static void printDatabase(List<Student> students) {
for (Student student: students) {
// here you'll use the toString from the Student POJO
System.out.println(student);
}
}
public static void getStudentById(List<Student> students, int idNumber) {
System.out.println(
students.stream().filter(student -> student.getIdNumber() == idNumber)
.findFirst().orElse(new Student(0, "John Doe", 0.0)));
}
public static void getStudentByIdV0(List<Student> students, int idNumber) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getIdNumber() == idNumber)
System.out.println(students.get(i));
}
}
}
getStudentById is looking in your list of student if it founds the right student with the id you passed. If no result is found, it create a John Doe Student... It will print a student in all case
getStudentByIdV0, is checking student by idNumber and print it if found.
this code may look a little bit more complex but you do not have to reinvent the wheel.
I need to print each element of an ArrayList. This ArrayList can be of type ArrayList or String. Further children of this list can also be of type list.
This is what I have written till now:
public class Logic extends AppCompatActivity {
ArrayList<Object> parent = new ArrayList<>();
ArrayList<Object> check = new ArrayList<>();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parent.add("list 1");
check.add("list 2");
check.add(parent);
parent.add(check);
for (int i = 0; i < parent.size(); i++) {
if (!(parent.get(i) instanceof ArrayList)) {
Log.e("test",(String) parent.get(i));
} else {
printDetails((ArrayList<?>) parent.get(i));
}
}
}
public synchronized void printDetails(ArrayList<?> child) {
for (int i = 0; i < child.size(); i++) {
if (!(child.get(i) instanceof ArrayList)) {
Log.e("test1", (String) child.get(i));
} else {
printDetails((ArrayList<?>) child.get(i));
}
}
}
}
But this prints all the abrupt values without any expected order. Expectation is that all the values should print serially. Like if the first element of a list is string it should first print the string else if it is a list if should print all values of this child list and if any element of the child list is again list it should print these values too and move back to the next element of parent list.
If this is the input:
ArrayList<Object> parent = new ArrayList<>();
ArrayList<Object> child1 = new ArrayList<>();
ArrayList<Object> subChild1= new ArrayList<>();
ArrayList<Object> nestedSubChild1= new ArrayList<>();
parent.add("list 1");
parent.add("list 1-2");
child1.add("childlist 2");
subChild1.add("sublist 1-1");
subChild1.add("sublist 1-2");
nestedSubChild1.add("nestedlist 1-1");
nestedSubChild1.add("nestedlist 1-2");
subChild1.add(nestedSubChild1);
child1.add(subChild1);
parent.add(child1);
I want this output
list 1
list 1-2
childlist 2
sublist 1-1
sublist 1-2
nestedlist 1-1
nestedlist 1-2
Another requirement is to use recursion so that the code will be efficient.
You can do so by creating a recursive method for printing out the lists
public static void printRecursiveList(List myList) {
for (Object obj : myList) {
if (obj instanceof List) {
printRecursiveList((List) obj); // call itself again since its a list
} else {
System.out.println(obj);
}
}
}
Now you can call it using :
parent.stream().forEach(i -> {
if (i instanceof List) {
printRecursiveList((List) i);
} else {
System.out.println(i);
}
});
Here we stream over the list parent and if the element is an instance of a list we invoke printRecursiveList(...) else we print out the element.
You can print required elements of Model class by overriding toString() method of an Object.
Support You got and ArrayList of Child Class, i.e. ArrayList<Child>:
- toString() can be implemented like:
class Child {
private String name;
.... some other variable
#Override
public String toString() {
return "Name: " + name + ": last updated: " +lastUpdated;
}
}
Now, when you print child.toString() It will print the result just like you want to print it.
I tested your code with static variables it compiles as you expected. See below:
import java.util.ArrayList;
import java.util.*;
public class MyClass {
static ArrayList<Object> parent = new ArrayList<>();
static ArrayList<Object> check = new ArrayList<>();
static ArrayList<Object> check2 = new ArrayList<>();
public static void main(String args[]) {
parent.add("list 1");
parent.add("list 2");
check.add("list 3");
check2.add("list 4");
check2.add("list 5");
check2.add("list 6");
check.add(check2);
//check.add(parent);
parent.add(check);
System.out.println(Arrays.deepToString(parent.toArray()));
System.out.println("-------");
for (int i = 0; i < parent.size(); i++) {
if (!(parent.get(i) instanceof ArrayList)) {
System.out.println("firstPart -> " + (String) parent.get(i));
}
else {
printDetails((ArrayList<?>) parent.get(i));
}
}
}
public static void printDetails(ArrayList<?> child) {
for (int i = 0; i < child.size(); i++) {
if (!(child.get(i) instanceof ArrayList)) {
System.out.println("lastPart -> " + (String) child.get(i));
} else {
printDetails((ArrayList<?>) child.get(i));
}
}
}
}
But i find that when i uncomment the commented block "check.add(parent);", it creates nested loop which generates java.lang.StackOverflowError.
By the way you can use
Arrays.deepToString(parent.toArray());
method if you only log the values in order.
Try this. It should work.
import java.util.ArrayList;
public class PrintArrayList
{
public static void main(String[] args)
{
ArrayList<Object> parent = new ArrayList<>();
ArrayList<Object> child1 = new ArrayList<>();
ArrayList<Object> subChild1= new ArrayList<>();
ArrayList<Object> nestedSubChild1= new ArrayList<>();
parent.add("list 1");
parent.add("list 1-2");
child1.add("childlist 2");
subChild1.add("sublist 1-1");
subChild1.add("sublist 1-2");
nestedSubChild1.add("nestedlist 1-1");
nestedSubChild1.add("nestedlist 1-2");
subChild1.add(nestedSubChild1);
child1.add(subChild1);
parent.add(child1);
print(parent);
}
public static void print(ArrayList<Object> arrayList)
{
for (Object element : arrayList)
{
if (element instanceof ArrayList)
{
print((ArrayList) element);
}
else
{
System.out.println(element);
}
}
}
}
I'd like to create a list, that stores the same kind of objects, for example:
class Card {
String title;
int point;
public Card(String t, int p) {
title = t;
point = p;
}
}
And I add some objects to the list:
list.add(new Card("Fred",3));
list.add(new Card("Fred",1));
list.add(new Card("Luke",5));
list.add(new Card("John",3));
How can I do the following tasks?
Create a new list, which contains the previous elements, but every title once, and counted their occurences. For example:
Fred 2
Luke 1
John 1
I can do the first part, I just make a HashSet, and overwrite the Song's 'equals' and 'hashCode' method. But I don't want to delete two with the same title completely.
Map<String, Card> cardsByTitle = new TreeMap<>();
void add(String title, int point) {
Card card = cardsByTitle.get(title);
if (card == null) {
card = new Card(title, point);
cardsByTitle.put(title, point);
} else {
card.point += point;
}
}
for (Card card : cardsByTitle.values) { }
Or something similar. TreeMap (i.o. HashMap) keeps the titles ordered.
You can implement your own List, that internally stores its elements in an ArrayList and keeps a Map of the number of Cards with a given title:
public static class CountingList extends AbstractList<Card>{
private final List<Card> list;
private final Map<String, Integer> counterMap;
public CountingList() {
this(10);
}
public CountingList(Collection<? extends Card> c) {
this(c.size());
addAll(c);
}
public CountingList(int initialCapacity) {
super();
this.list = new ArrayList<>(initialCapacity);
this.counterMap = new HashMap<>(initialCapacity);
}
#Override
public boolean add(Card e) {
Integer count = counterMap.get(e.title);
String key = e.title;
if (count == null) {
list.add(e);
count = 0;
}
counterMap.put(key, ++count);
return count == 1;
}
#Override
public void add(int index, Card element) {
throw new UnsupportedOperationException();
}
#Override
public Card remove(int index) {
throw new UnsupportedOperationException();
}
#Override
public Card get(int index) {
return list.get(index);
}
#Override
public int size() {
return list.size();
}
public int getNumberOfCards(int index){
return getNumberOfCards(list.get(index).title);
}
public int getNumberOfCards(String title){
Integer count = counterMap.get(title);
return count == null ? 0 : count;
}
#Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append('[');
for (Card c : this) {
b.append(c.title).append('(').append(getNumberOfCards(c.title)).append(')');
}
b.append(']');
return b.toString();
}
}
Then you can use this List like so:
public static void main(String[] args) {
List<Card> list = new ArrayList<>();
list.add(new Card("Fred",3));
list.add(new Card("Fred",1));
list.add(new Card("Luke",5));
list.add(new Card("John",3));
CountingList countingList = new CountingList(list);
System.out.println(countingList);
countingList.add(new Card("Tom", 1));
countingList.add(new Card("Tom", 10));
countingList.add(new Card("Tom", 15));
System.out.println(countingList);
countingList.add(new Card("Fred", 4));
System.out.println(countingList);
}
Note, that inserting elements at a given index and removing elements is not yet supported for this List.
To get the number of the cards with a given title, you can call CountingList#getNumberOfCards.
I had to write a program to do LZWDecode and I decided to use LinkedList to write the LZWDecode program below but I want to convert it to an ArrayList. Anyone have idea on how I can convert the LinkedList to an ArrayList to make it simpler.
Thanks.
import java.util.*;
public class LZWDecoder {
private final int CLEAR_TABLE=256;
private final int END_OF_DATA=257;
private final int TABLE_SIZE=4096;
private static LinkedList<Integer> input = new LinkedList<Integer>();
#SuppressWarnings("unchecked")
private LinkedList<Integer>[] table
= new LinkedList[TABLE_SIZE];
private LinkedList<Integer> temp = new LinkedList<Integer>();
private int index = 258;
private LinkedList<String> trace = new LinkedList<String>();
private boolean view = true;
private void enterData() {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the Input Code (EOD = 257):");
int n=0;
while(n!=END_OF_DATA && scan.hasNextInt()){
n = scan.nextInt();
//System.out.println("Adding "+n);
input.add(n);
}
System.out.println("Decoding...\nOutput:");
String code="";
for(int i=0; i<input.size(); i++) {
code+=input.get(i)+" ";
}
trace.add("\nInput: "+code);
//test
/*
while(!input.isEmpty()) {
System.out.println(input.remove());
}
*/
}
private void reset() {
trace.add("Clearing...");
//table.clear();
for(int i=0; i<TABLE_SIZE;i++) {
table[i] = new LinkedList<Integer>();
}
}
private void decode(int c) {
switch(c) {
case CLEAR_TABLE:
trace.add("decode\t"+CLEAR_TABLE+"->[256]");
reset();
break;
case END_OF_DATA:
trace.add("decode\t"+END_OF_DATA+"->[257]");
trace.add("Decoding finished.");
break;
default:
if(c<256) {
trace.add("decode\t"+c+"->["+c+"]");
if(!temp.isEmpty()) append(c);
emit(c);
add(temp);
} else {
trace.add("decode\t"+c+"->["+printTableNode(table[c])+"]");
if(!temp.isEmpty()) append(table[c].get(0));
emit(c, table[c]);
add(temp);
}
}
}
private void emit(int n, LinkedList<Integer> c) {
//int [] a=new int[c.size()];
temp=new LinkedList<Integer>();
for(int i=0; i<c.size(); i++) {
//a[i]=c.get(i);
System.out.print(c.get(i)+" ");
temp.add(c.get(i));
}
trace.add("emit\t"+n+"->"+"["+printTableNode(c)+"]");
}
private void emit(int c) {
//print out output
temp=new LinkedList<Integer>();
temp.add(c);
trace.add("emit\t"+c+"->"+"["+c+"]");
System.out.print(c+" ");
}
/*
private void add(int c) {
//added to table is copied to temp
table[index].add(c);
temp = (LinkedList)table[index].clone();
trace.add("add\t"+index+"->["+printTableNode(table[index])+"]");
}
*/
private void add(LinkedList<Integer> c) {
for(int i=0; i<c.size();i++) {
//temp.add(c.get(i));
table[index].add(c.get(i));
}
trace.add("add\t"+index+"->["+printTableNode(table[index])+"]");
}
private void append(int c) {
//table[c].add(12);//add what?
//temp.add(c);
table[index].add(c);
trace.add("append\t"+index+"->["+printTableNode(table[index])+"]");
index++;
}
private String printTableNode(LinkedList l) {
String list="";
for(int i=0; i<l.size();i++) {
list+=l.get(i);
if(i<l.size()-1) {
list+=", ";
}
}
return list;
}
private void printTrace() {
System.out.print("Printing Trace...");
for(int i=0; i<trace.size(); i++) {
System.out.println(trace.get(i));
}
}
public static void main(String[] args) {
// TODO code application logic here
LZWDecoder d = new LZWDecoder();
d.enterData();
while(!input.isEmpty()) {
d.decode(input.remove());
}
System.out.print("\n\n");
d.printTrace();
}
}
LinkedList<String> ll= new LinkedList<String>();
ll.add("A");
ll.add("B");
ll.add("C");
ll.add("D");
List<String> myAL = new ArrayList<String>(ll);
for (Object alObject : myAL)
System.out.println(alObject);
So as you can easily convert the LinkedList to ArrayList bu using its constructor with passing Collection in it.
Hope it will clear your doubt.
Question is not clear enough.
Do you want to use ArrayList instead of Linked List?
Or do you want to convert a Linked List to an ArrayList?
First of all please declare variables on their interface not on implementation,
i.e
LinkedList<Integer>[] table = new LinkedList[TABLE_SIZE];
Instead use
List<Integer>[] table = new LinkedList[TABLE_SIZE];
Please provide a little more details on what you really looking for ....
If you want an array List from another collection, do this,
List<T> t = new ArrayList<>();
t.addAll(linkedList);
Regards
Lyju
I need to implement makeMatches method in stable marriage class.
Code needs to follow this algorithm:
set each person to be free;
while (some man m with a nonempty preference list is free) {
w = first woman on m's list;
if (some man p is engaged to w) {
set p to be free
}
set m and w to be engaged to each other
for (each successor q of m on w's list) {
delete w from q's preference list
delete q from w's preference list
}
}
However I can not find my error. The program makes couples depend on their first choices but after that doesn't continue and set couples.
Could you look at my code and tell me what is wrong with it?
Person.java is provided from instructor:
import java.util.*;
public class Person {
public static final int NOBODY = -1;
private String name;
private List<Integer> preferences;
private List<Integer> oldPreferences;
private int partner;
public Person(String name) {
this.name = name;
preferences = new ArrayList<Integer>();
oldPreferences = new ArrayList<Integer>();
erasePartner();
}
public void erasePartner() {
partner = NOBODY;
}
public boolean hasPartner() {
return partner != NOBODY;
}
public int getPartner() {
return partner;
}
public void setPartner(int partner) {
this.partner = partner;
}
public String getName() {
return name;
}
public boolean hasChoices() {
return !preferences.isEmpty();
}
public int getFirstChoice() {
return preferences.get(0);
}
public void addChoice(int person) {
preferences.add(person);
oldPreferences.add(person);
}
public List<Integer> getChoices() {
return preferences;
}
public int getPartnerRank() {
return oldPreferences.indexOf(partner) + 1;
}
}
And StablaMarriage.java is the one I need to implement code in.
import java.io.*;
import java.util.*;
public class StableMarriage {
public static final String LIST_END = "END";
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.print("What is the input file? ");
String fileName = console.nextLine();
Scanner input = new Scanner(new File(fileName));
System.out.println();
List<Person> men = readHalf(input);
List<Person> women = readHalf(input);
makeMatches(men, women);
writeList(men, women, "Matches for men");
writeList(women, men, "Matches for women");
}
public static Person readPerson(String line) {
int index = line.indexOf(":");
Person result = new Person(line.substring(0, index));
Scanner data = new Scanner(line.substring(index + 1));
while (data.hasNextInt()) {
result.addChoice(data.nextInt());
}
return result;
}
public static List<Person> readHalf(Scanner input) {
List<Person> result = new ArrayList<Person>();
String line = input.nextLine();
while (!line.equals(LIST_END)) {
result.add(readPerson(line));
line = input.nextLine();
}
return result;
}
public static void makeMatches(List<Person> list1, List<Person> list2) {
for (Person eachMan : list1){
eachMan.setPartner(-1);
}
for (Person eachWoman : list2){
eachWoman.setPartner(-1);
}
for (Person man : list1){
for (Person woman : list2){
while (man.hasChoices() && !man.hasPartner()){
int choosenWoman = man.getFirstChoice();
for (Person p : list1){
if (p.getPartner()== choosenWoman){
p.setPartner(-1);
}
}
man.setPartner(choosenWoman);
list2.get(choosenWoman).setPartner(list1.indexOf(man));
List manList= man.getChoices();
List womanList = woman.getChoices();
for (int q= womanList.size()-1; q>=0; q--){
if(q>womanList.indexOf(list1.indexOf(man)))
manList.remove(manList.indexOf(man.getFirstChoice()));
womanList.remove(q);
}
}
}
}
}
public static void writeList(List<Person> list1, List<Person> list2,
String title) {
System.out.println(title);
System.out.println("Name Choice Partner");
System.out.println("--------------------------------------");
int sum = 0;
int count = 0;
for (Person p : list1) {
System.out.printf("%-15s", p.getName());
if (!p.hasPartner()) {
System.out.println(" -- nobody");
} else {
int rank = p.getPartnerRank();
sum += rank;
count++;
System.out.printf("%4d %s\n", rank,
list2.get(p.getPartner()).getName());
}
}
System.out.println("Mean choice = " + (double) sum / count);
System.out.println();
}
}