Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: PLEASE HELP WITH JAVA ARRAYLISTS! Thanks.

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PLEASE HELP WITH JAVA ARRAYLISTS! Thanks.

    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!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: PLEASE HELP WITH JAVA ARRAYLISTS! Thanks.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

Similar Threads

  1. boolean arraylists?
    By Scorks in forum Collections and Generics
    Replies: 1
    Last Post: October 15th, 2013, 11:47 AM
  2. [SOLVED] ArrayLists - Getting index of parameters of objects of ArrayLists.
    By mwebb in forum Object Oriented Programming
    Replies: 1
    Last Post: February 16th, 2012, 07:39 PM
  3. Somehow it's not adding to an ArrayList of ArrayLists.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 05:09 PM
  4. Creating ArrayLists
    By Khadafi in forum Java Theory & Questions
    Replies: 15
    Last Post: January 12th, 2012, 06:18 PM
  5. Quick question, arraylists
    By Intensity` in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 16th, 2011, 08:47 PM

Tags for this Thread