1

A friend of mine asked me to give him an example of how to create an array list as well as add, display, delete, and modify it I've already made methods for everything except modify so some help?

public class Manager {
    private static ArrayList<String> list = new ArrayList<String>();
    private static BPScanner kb = new BPScanner();
    private static String name;
    public static void main(String[] args) {
        while (true) {
            String input = kb.getMenuStringFromUser("List Manager","Add", "Delete", "Modify", 
                    "Display", "Quit");
            if (input.equals("Quit"))
                break;
            if (input.equals("Add")) {
                add();
            } else if (input.equals("Display")) {
                display();
            }else if(input.equals("Delete")){
                delete();
            }

        }
    }

    public static void add() {
        do{
            name= kb.getStringFromUser("Enter name: ");
        }while(!isAlpha(name));
        list.add(name);
    }

    private static boolean isAlpha(String name){
        char c;
        for(int i=0; i<name.length(); i++){
            c=name.charAt(i);
            if('A'<=c&&c<='Z'||'a'<=c&&c<='z'||c==' '){
            }else{
                return false;
            }
        }
        return true;
    }

    public static void display() {
        System.out.println("\nList:");
        for (int i=0; i < list.size(); i++) {
            kb.getStringFromUser(list.get(i));
            //System.out.println(name);
        }
        String input = kb.getStringFromUser("\nContinue (y/n)? ");
        if (input.startsWith("n")) System.exit(0);
    }

    public static void delete(){
        list.remove(name);
    }

    public static void modify(){

    }
}

I literally have no idea of what to write to get it to modify the names put into the array, so any ideas?

2
  • docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html Commented May 6, 2014 at 6:28
  • For the problem that you mentioned about not displaying the whole list try this: Remove anything from the loop and put this in: System.out.println(list.get(i)); Commented May 6, 2014 at 7:14

5 Answers 5

2

Lets define the modify function as

public static void modify(String toModify, String modifyAs) {
    int pos = ar.indexOf(toModify);
    ar.set(pos, modifyAs);
}

toModify is Variable holds the item to modify and modifyAs holds the new item to add

Sign up to request clarification or add additional context in comments.

Comments

1

Simply just pass an index parameter. Than you use that index to modify the element which is on position index. In order to change a certain value in the list you can use the list.set(index, element); (in your case the element is string) function.

public static void modify(int index)
{
     string nextName = kb.getStringFromUser("Enter name: ");
     list.set(index, nextName);
}

Comments

0

Below code shows you how to add, view and delete an element from a particular location in arraylist.

import java.util.ArrayList;

public class RemoveElementFromArrayListExample {

  public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();

    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");

    /*
      To remove an element from the specified index of ArrayList use
      Object remove(int index) method.
      It returns the element that was removed from the ArrayList.
    */
    Object obj = arrayList.remove(1);
    System.out.println(obj + " is removed from ArrayList");

    System.out.println("ArrayList contains...");
    //display elements of ArrayList
    for(int index=0; index < arrayList.size(); index++)
      System.out.println(arrayList.get(index));

  }
}

2 Comments

I know my question is how do I modify an element in the array like say change ben to Ben?
you can replace the element, checkout set(int index, E element) in List Interface. accept the answer if you get the desired output.
0

From java docs you can use ,

Replaces the element at the specified position in this list with the specified element (optional operation).

Parameters:

index - index of the element to replace

element - element to be stored at the specified position

list.set(your_index,element);

Comments

0
public static void modify() {
    String origName = kb.getStringFromUser("Which name do you want to modify: ");
    if (list.contains(origName)) {
        int index = list.indexOf(name);
        name = kb.getStringFromUser("Enter new name: ");
        list.set(index, name);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.