Question

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?

Was it helpful?

Solution 2

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);
}

OTHER TIPS

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

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));

  }
}

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);
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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top