Example usage for java.util List set

List of usage examples for java.util List set

Introduction

In this page you can find the example usage for java.util List set.

Prototype

E set(int index, E element);

Source Link

Document

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

Usage

From source file:MainClass.java

public static void main(String[] a) {
    List list = Arrays.asList(new String[] { "A", "B", "C", "D" });
    list.set(2, "X");
    System.out.println(list);/*from  w  w  w.j  av  a  2 s  .c o m*/
}

From source file:Main.java

public static void main(String[] a) {
    List<String> list = Arrays.asList(new String[] { "A", "B", "C", "D" });
    list.set(2, "X");
    System.out.println(list);//from  w w w .ja va 2  s .c  o  m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    list = Collections.unmodifiableList(list);

    try {//from   www  . ja va2s  .  c  o m
        list.set(0, "new value");
    } catch (UnsupportedOperationException e) {
        System.out.println(e.getMessage());
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    list = Collections.unmodifiableList(list);

    try {//from   w ww  .  j av a  2  s  .  c o m
        list.set(0, "new value");
    } catch (UnsupportedOperationException e) {

    }

    Set set = new HashSet(stuff);
    set = Collections.unmodifiableSet(set);

    Map map = new HashMap();
    map = Collections.unmodifiableMap(map);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> stringList = Arrays.asList(args);
    System.out.println("before: " + stringList);
    for (int i = 0; i < stringList.size(); ++i) {
        stringList.set(i, stringList.get(i).replaceAll("\\s+", ""));
    }/*ww w .ja va2  s  . c  o m*/
    System.out.println("after : " + stringList);
}

From source file:com.l2jfree.tools.ProjectSettingsSynchronizer.java

public static void main(String[] args) throws IOException {
    final File src = new File(".").getCanonicalFile();
    System.out.println("Copying from: " + src);
    System.out.println();/*from   www . ja  v  a2  s.c  om*/

    final List<File> destinations = new ArrayList<File>();
    for (File dest : src.getParentFile().listFiles()) {
        if (dest.isHidden() || !dest.isDirectory())
            continue;

        destinations.add(dest);
        System.out.println("Copying to: " + dest);
    }
    System.out.println();

    // .project
    System.out.println(".project");
    System.out.println("================================================================================");
    {
        final List<String> lines = FileUtils.readLines(new File(src, ".project"));

        for (File dest : destinations) {
            lines.set(2, lines.get(2).replaceAll(src.getName(), dest.getName()));
            writeLines(dest, ".project", lines);
            lines.set(2, lines.get(2).replaceAll(dest.getName(), src.getName()));
        }
    }
    System.out.println();

    // .classpath
    System.out.println(".classpath");
    System.out.println("================================================================================");
    {
        final List<String> lines = FileUtils.readLines(new File(src, ".classpath"));

        for (File dest : destinations) {
            if (dest.getName().endsWith("-main") || dest.getName().endsWith("-datapack")) {
                final ArrayList<String> tmp = new ArrayList<String>();

                for (String line : lines)
                    if (!line.contains("classpathentry"))
                        tmp.add(line);

                writeLines(dest, ".classpath", tmp);
                continue;
            }

            writeLines(dest, ".classpath", lines);
        }
    }
    System.out.println();

    // .settings
    System.out.println(".settings");
    System.out.println("================================================================================");
    for (File settingsFile : new File(src, ".settings").listFiles()) {
        if (settingsFile.getName().endsWith(".prefs")) {
            System.out.println(".settings/" + settingsFile.getName());
            System.out.println(
                    "--------------------------------------------------------------------------------");

            final List<String> lines = FileUtils.readLines(settingsFile);

            if (lines.get(0).startsWith("#"))
                lines.remove(0);

            for (File dest : destinations) {
                writeLines(new File(dest, ".settings"), settingsFile.getName(), lines);
            }
            System.out.println();
        }
    }
    System.out.println();
}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = new ArrayList<Integer>();

    for (Integer i : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7))
        numbers.add(i);//from   w  w  w .j av  a  2  s .c o  m
    printList(numbers); // 0,1,2,3,4,5,6,7

    // replaces each element with twice its value
    for (int index = 0; index < numbers.size(); index++) {
        numbers.set(index, numbers.get(index) * 2);
    }
    printList(numbers);

    // does nothing because list is not being changed
    for (Integer number : numbers) {
        number++;
    }
    printList(numbers);

    // same as above -- just different syntax
    for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext();) {
        Integer number = iter.next();
        number++;
    }
    printList(numbers);

    for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext();) {
        Integer number = iter.next();
        iter.add(number + 1);
    }
    printList(numbers);

    for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext();) {
        Integer number = iter.next();
        if (number % 2 == 0) // if number is even
            iter.remove(); // remove it from the collection
    }
    printList(numbers); // 1,3,5,7,9,11,13,15

    // ListIterator<?> has a "set" method to replace elements
    for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext();) {
        Integer number = iter.next();
        iter.set(number / 2); // divide each element by 2
    }
    printList(numbers); // 0,1,2,3,4,5,6,7

    // Use Java 8 Lambda
    List<Integer> list = numbers;
    list.stream().forEach(elem -> System.out.println("element " + elem));

}

From source file:Main.java

public static List<String> merge(final List<String> list, final int index) {
    if (list.isEmpty()) {
        throw new IndexOutOfBoundsException("Cannot merge empty list");
    } else if (index + 1 >= list.size()) {
        throw new IndexOutOfBoundsException("Cannot merge last element");
    } else {/*from   www.j a  va2  s .c o m*/
        final List<String> result = new ArrayList<String>(list);
        result.set(index, list.get(index) + list.get(index + 1));
        result.remove(index + 1);
        return result;
    }
}

From source file:Main.java

public static <T> void exchange(List<T> a, int i, int j) {
    T temp = a.get(i);//from  www  .j a va2s  . c o m
    a.set(i, a.get(j));
    a.set(j, temp);
}

From source file:Main.java

public static int setOne(int data, int set, int index) {
    List<Integer> list = getList(data);
    list.set(index, set);
    return getInt(list);
}