Solve this in JAVA Suppose the names \"Bob\", \"Dan\", and \"Ben\", are stored a
ID: 3550351 • Letter: S
Question
Solve this in JAVA
Suppose the names "Bob", "Dan", and "Ben", are stored as shown below: ----- ----- head -->| B |---------------->| D | ----- ----- | | | ----- ----- | ----- -->| Ben |-->| Bob | -->| Dan | ----- ----- ----- If the names "Sue" and "Sarah" are added, it looks like this: ----- ----- ----- head -->| B |---------------->| D |-------->| S | ----- ----- ----- | | | | ----- ----- | ----- | ------- ----- -->| Ben |-->| Bob | -->| Dan | -->| Sarah |-->| Sue | ----- ----- ----- ------- ----- Details: Create a class called Index (not generic) that stores strings and implements the structure shown above. The top-level list is kept in sorted order, as is each sublist. You may not use the LinkedList class. You should create your own list nodes and link them together as shown in the illustration. Your class should support these methods: add - adds a new string. remove - removes a string. find - finds a string toString - prints the list like this (using the first list above as input): B Ben Bob D Dan main - demonstrates the methods of your Index class
Explanation / Answer
import java.util.ArrayList;
import java.util.Collections;
//The only way I can think to do this is to create a List.
//So here is the implementation
public class Index {
ArrayList<String> nameList;
public ArrayList<String> getNameList() {
return nameList;
}
public void setNameList(ArrayList<String> nameList) {
this.nameList = nameList;
}
public void add(String stringToAdd){
nameList.add(stringToAdd);
}
public boolean remove(String stringToRemove){
boolean flag = false;
for(int i=0; i<nameList.size(); i++){
if(nameList.get(i).equalsIgnoreCase(stringToRemove)){
nameList.remove(i);
flag = true;
}
}
return flag;
}
public boolean find(String stringToFind){
boolean flag = false;
for(int i=0; i<nameList.size(); i++){
if(nameList.get(i).equalsIgnoreCase(stringToFind)){
flag = true;
}
}
return flag;
}
public String toString(){
Collections.sort(nameList);
StringBuilder builder = new StringBuilder();
char currentStartChar='0'; // some arbitrary value to initilize
for(String nextName: nameList){
char nextStartChar = nextName.charAt(0);
if(currentStartChar!=nextStartChar){
currentStartChar = nextStartChar;
builder.append(String.valueOf(currentStartChar).toUpperCase());
builder.append(" ");
builder.append(" ");
builder.append(nextName);
builder.append(" ");
}
else{
builder.append(" ");
builder.append(nextName);
builder.append(" ");
}
}
return builder.toString();
}
public static void main(String[] args) {
Index index = new Index();
ArrayList<String> nameList = new ArrayList<String>();
nameList.add("Ben");
nameList.add("Bob");
nameList.add("Dan");
nameList.add("Sarah");
nameList.add("Sue");
nameList.add("John");
nameList.add("Jessica");
nameList.add("Rick");
index.setNameList(nameList);
System.out.println(index.toString());
index.add("Ringo");
System.out.println(" After adding to list");
System.out.println(index.toString());
index.remove("Jessica");
index.remove("John");
System.out.println(" After removing from list");
System.out.println(index.toString());
boolean findBen = index.find("Ben");
if(findBen){
System.out.println("Ben is there in the list");
}
else{
System.out.println("Ben is not in list");
}
boolean findMike = index.find("Mike");
if(findMike){
System.out.println("Mike is there in the list");
}
else{
System.out.println("Mike is not in list");
}
}
}