Here is the assignment:
Write a program that does the following things.
1. In main, create an ArrayList of the names in our class (Strings are objects)
If you are using Scanner with a name with a space use nextLine()
2. Use the size method to print out the size of the ArrayList
3. Test all of the following methods that you will write.
4. Write a method printList() to print out the list numbered vertically as seen at left.
public static void printList(ArrayList<String> x)
5. Write a method to delete all names that start with a given character
public static void removeCh(ArrayList<String> x, char ch)
**Be careful of duplicates***
6. Write a method to add all the elements in a String array to the ArrayList
public static void addArray(ArrayList<String> x, String[]names)
7. Write a method to replace a given name with another name
public static void replaceName(ArrayList<String> x, String oldOne,
String newOne)
8. Write a method to remove duplicates
public static void removeDup(ArrayList<String> x)
__________________________________________________ __________________________________
This is the code I have so far:
import java.util.ArrayList;
public class Students
public static void main(String[] args)
{
ArrayList<Students> names = new ArrayList();
names.add("y");
names.add("w");
names.add("f");
names.add("v");
names.add("d");
names.add("r");
names.add("u");
names.add("t");
names.add("i");
names.add("o");
names.add("n");
public static void printList(ArrayList<String> st)
{
for(int v=0; v<st.size(); v++) {
System.out.println(v + st.get(v));
}
}
public static void removeCh(ArrayList<String> st, char hh)
{
for(int g=0; g<st.size(); g++)
{
if(st.get(g).charAt(0) == hh)
st.remove(g);
g--;
}
}
public static void addArray(ArrayList<String> st, String[]names)
{
for (int r=0; r<names.length; r++)
{
st.add(names[r]);
}
}
public static void replaceName(ArrayList<String> st, String oldOne,String newOne)
{
for (int e=0;e<st.size();e++)
{
if ((st.get(e)).equals(oldOne))
st.set(e, newOne);
}
}
public static void removeDup(ArrayList<String> st)
{
for (int d=0;d<st.size();d++)
{
for(int b=0;f<st.size();f++)
{
if((st.get(d)).equals(st.get(f)) && d!=b)
{
st.remove(d);
}
}
}
}
}
What should I add to fix it? PLEASE PLEASE PLEASE HELP!