Example usage for java.util Vector removeElementAt

List of usage examples for java.util Vector removeElementAt

Introduction

In this page you can find the example usage for java.util Vector removeElementAt.

Prototype

public synchronized void removeElementAt(int index) 

Source Link

Document

Deletes the component at the specified index.

Usage

From source file:Main.java

public static void main(String args[]) {
    Vector v = new Vector(5);
    for (int i = 0; i < 10; i++) {
        v.add(0, i);/*from ww  w  .j av a  2 s  .c o  m*/
    }
    System.out.println(v.capacity());
    System.out.println(v);

    v.remove(1);

    v.removeElementAt(2);

    System.out.println(v);
    System.out.println(v.capacity());

}

From source file:Main.java

public static void main(String[] args) {

    Vector<Integer> vec = new Vector<Integer>(4);

    vec.add(4);/*from w  ww  .j av  a 2  s .  c o m*/
    vec.add(3);
    vec.add(2);
    vec.add(1);

    // lets remove element at index 2
    System.out.println("Remove element at index 2");
    vec.removeElementAt(2);

    for (Integer number : vec) {
        System.out.println("Number = " + number);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Vector vector = new Vector();
    vector.addElement(new Integer(5));
    vector.addElement(new Float(-14.14f));

    System.out.println(vector);/*from www  .j  av  a2s .  c o  m*/

    String s = new String("String to be inserted");
    vector.insertElementAt(s, 1);
    System.out.println(vector);

    vector.removeElementAt(2);
    System.out.println(vector);
}

From source file:Main.java

/**
 * Resolve a link relative to an absolute base.  The path to resolve could
 * itself be absolute or relative.//from w  w  w .  ja  v a2  s  .  co m
 * 
 * e.g.
 * resolvePath("http://www.server.com/some/dir", "../img.jpg");
 *  returns http://www.server.com/some/img.jpg
 * 
 * @param base The absolute base path
 * @param link The link given relative to the base
 * @return 
 */
public static String resolveLink(String base, String link) {
    String linkLower = link.toLowerCase();
    int charFoundIndex;

    charFoundIndex = linkLower.indexOf("://");
    if (charFoundIndex != -1) {
        boolean isAllChars = true;
        char cc;
        for (int i = 0; i < charFoundIndex; i++) {
            cc = linkLower.charAt(i);
            isAllChars &= ((cc > 'a' && cc < 'z') || (cc > '0' && cc < '9') || cc == '+' || cc == '.'
                    || cc == '-');
        }

        //we found :// and all valid scheme name characters before; path itself is absolute
        if (isAllChars) {
            return link;
        }
    }

    //Check if this is actually a data: link which should not be resolved
    if (link.startsWith("data:")) {
        return link;
    }

    if (link.length() > 2 && link.charAt(0) == '/' && link.charAt(1) == '/') {
        //we want the protocol only from the base
        String resolvedURL = base.substring(0, base.indexOf(':') + 1) + link;
        return resolvedURL;
    }

    if (link.length() > 1 && link.charAt(0) == '/') {
        //we should start from the end of the server
        int serverStartPos = base.indexOf("://") + 3;
        int serverFinishPos = base.indexOf('/', serverStartPos + 1);
        return base.substring(0, serverFinishPos) + link;
    }

    //get rid of query if it's present in the base path
    charFoundIndex = base.indexOf('?');
    if (charFoundIndex != -1) {
        base = base.substring(0, charFoundIndex);
    }

    //remove the filename component if present in base path
    //if the base path ends with a /, remove that, because it will be joined to the path using a /
    charFoundIndex = base.lastIndexOf(FILE_SEP);
    base = base.substring(0, charFoundIndex);

    String[] baseParts = splitString(base, FILE_SEP);
    String[] linkParts = splitString(link, FILE_SEP);

    Vector resultVector = new Vector();
    for (int i = 0; i < baseParts.length; i++) {
        resultVector.addElement(baseParts[i]);
    }

    for (int i = 0; i < linkParts.length; i++) {
        if (linkParts[i].equals(".")) {
            continue;
        }

        if (linkParts[i].equals("..")) {
            resultVector.removeElementAt(resultVector.size() - 1);
        } else {
            resultVector.addElement(linkParts[i]);
        }
    }

    StringBuffer resultSB = new StringBuffer();
    int numElements = resultVector.size();
    for (int i = 0; i < numElements; i++) {
        resultSB.append(resultVector.elementAt(i));
        if (i < numElements - 1) {
            resultSB.append(FILE_SEP);
        }
    }

    return resultSB.toString();
}

From source file:net.aepik.alasca.gui.util.LoadFileFrame.java

/**
 * Load a file.//from w ww.  ja v a2 s  .co  m
 */
public boolean loadFile(String filename, String syntaxe) {
    if (!(new File(filename)).exists()) {
        this.errorMessage = "Le fichier n'existe pas.";
        return false;
    }
    try {
        SchemaSyntax syntax = Schema.getSyntax(syntaxe);
        SchemaFile schemaFile = Schema.createAndLoad(syntax, filename, true);
        Schema schema = schemaFile.getSchema();
        if (schema == null) {
            String message = "";
            if (schemaFile.isError()) {
                message += "\n\n" + "Line " + schemaFile.getErrorLine() + ":\n" + schemaFile.getErrorMessage();
            }
            this.errorMessage = "Le format du fichier est incorrect." + message;
            return false;
        }
        if (manager.isSchemaIdExists((new File(filename)).getName())) {
            this.errorMessage = "Le fichier est dj ouvert.";
            return false;
        }
        Vector<String> files = Pref.getVector(Pref.PREF_LASTOPENFILES);
        Vector<String> syntaxes = Pref.getVector(Pref.PREF_LASTOPENSYNTAXES);
        int index = files.indexOf(filename);
        if (index >= 0) {
            files.removeElementAt(index);
            syntaxes.removeElementAt(index);
        }
        files.add(filename);
        syntaxes.add(syntaxe);
        if (files.size() > 10) {
            files.removeElementAt(0);
            syntaxes.removeElementAt(0);
        }
        Pref.set(Pref.PREF_LASTOPENFILES, files.toArray(new String[0]));
        Pref.set(Pref.PREF_LASTOPENSYNTAXES, syntaxes.toArray(new String[0]));
        manager.addSchema((new File(filename)).getName(), schema);
    } catch (Exception e) {
        e.printStackTrace();
        this.errorMessage = "Une erreur inattendue est survenue.";
        return false;
    }
    return true;
}

From source file:azkaban.common.utils.Utils.java

/**
 * Read in content of a file and get the last *lineCount* lines. It is
 * equivalent to *tail* command/*from   www  . j a v  a 2s  .co  m*/
 * 
 * @param filename
 * @param lineCount
 * @param chunkSize
 * @return
 */
public static Vector<String> tail(String filename, int lineCount, int chunkSize) {
    try {
        // read in content of the file
        RandomAccessFile file = new RandomAccessFile(filename, "r");
        // destination vector
        Vector<String> lastNLines = new Vector<String>();
        // current position
        long currPos = file.length() - 1;
        long startPos;
        byte[] byteArray = new byte[chunkSize];

        // read in content of the file in reverse order
        while (true) {
            // read in from *fromPos*
            startPos = currPos - chunkSize;

            if (startPos <= 0) { // toward the beginning of the file
                file.seek(0);
                file.read(byteArray, 0, (int) currPos); // only read in
                                                        // curPos bytes
                parseLinesFromLast(byteArray, 0, (int) currPos, lineCount, lastNLines);
                break;
            } else {
                file.seek(startPos);
                if (byteArray == null)
                    byteArray = new byte[chunkSize];
                file.readFully(byteArray);
                if (parseLinesFromLast(byteArray, lineCount, lastNLines)) {
                    break; // we got the last *lineCount* lines
                }

                // move the current position
                currPos = startPos; // + lastLine.getBytes().length;
            }
        }

        // there might be lineCount + 1 lines and the first line (now it is the last line)
        // might not be complete
        for (int index = lineCount; index < lastNLines.size(); index++)
            lastNLines.removeElementAt(index);

        // reverse the order of elements in lastNLines
        Collections.reverse(lastNLines);
        return lastNLines;
    } catch (Exception e) {
        return null;
    }

}

From source file:alice.tuprolog.lib.OOLibrary.java

private static Method mostSpecificMethod(Vector<Method> methods) throws NoSuchMethodException {
    for (int i = 0; i != methods.size(); i++) {
        for (int j = 0; j != methods.size(); j++) {
            if ((i != j) && (moreSpecific(methods.elementAt(i), methods.elementAt(j)))) {
                methods.removeElementAt(j);
                if (i > j)
                    i--;// www.j a v  a 2 s  .c  o m
                j--;
            }
        }
    }
    if (methods.size() == 1)
        return methods.elementAt(0);
    else
        throw new NoSuchMethodException(">1 most specific method");
}

From source file:alice.tuprolog.lib.OOLibrary.java

private static Constructor<?> mostSpecificConstructor(Vector<Constructor<?>> constructors)
        throws NoSuchMethodException {
    for (int i = 0; i != constructors.size(); i++) {
        for (int j = 0; j != constructors.size(); j++) {
            if ((i != j) && (moreSpecific(constructors.elementAt(i), constructors.elementAt(j)))) {
                constructors.removeElementAt(j);
                if (i > j)
                    i--;/*from ww w  .  jav a  2s . co m*/
                j--;
            }
        }
    }
    if (constructors.size() == 1)
        return constructors.elementAt(0);
    else
        throw new NoSuchMethodException(">1 most specific constructor");
}

From source file:ca.uqac.info.trace.generation.RandomTraceGenerator.java

@Override
public EventTrace generate() {
    EventTrace trace = new EventTrace();
    // We choose the number of messages to produce
    int n_messages = super.nextInt(m_maxMessages - m_minMessages) + m_minMessages;
    for (int i = 0; i < n_messages; i++) {
        Node n = trace.getNode();
        Vector<String> available_params = new Vector<String>();
        // We produce the list of available parameters for this message
        for (int j = 0; j < m_numParameters; j++)
            available_params.add("p" + j);
        // We choose the number of param-value pairs to generate
        int arity = super.nextInt(m_maxArity - m_minArity) + m_minArity;
        for (int j = 0; j < arity; j++) {
            // We generate as many param-value pairs as required
            int index = super.nextInt(available_params.size());
            int value = super.nextInt(m_domainSize);
            if (m_minArity == 1 && m_maxArity == 1 && m_flatten) {
                // For traces of messages with fixed arity = 1, we
                // simply put the value as the text child of the "Event"
                // element
                n.appendChild(trace.createTextNode(new Integer(value).toString()));
            } else {
                Node el = trace.createElement("p" + index);
                el.appendChild(trace.createTextNode(new Integer(value).toString()));
                n.appendChild(el);// ww  w.  ja va2 s.  c om
                if (!m_isMultiValued) {
                    // If event should not be multi-valued, then we remove
                    // the chosen parameter from the list of available choices
                    available_params.removeElementAt(index);
                }
            }
        }
        Event e = new Event(n);
        trace.add(e);
    }
    return trace;
}

From source file:Maze.java

/**
 * This method randomly generates the maze.
 *///from  w  ww.  j a  va2 s. c  o m
private void createMaze() {
    // create an initial framework of black squares.
    for (int i = 1; i < mySquares.length - 1; i++) {
        for (int j = 1; j < mySquares[i].length - 1; j++) {
            if ((i + j) % 2 == 1) {
                mySquares[i][j] = 0;
            }
        }
    }
    // initialize the squares that can be either black or white 
    // depending on the maze.
    // first we set the value to 3 which means undecided.
    for (int i = 1; i < mySquares.length - 1; i += 2) {
        for (int j = 1; j < mySquares[i].length - 1; j += 2) {
            mySquares[i][j] = 3;
        }
    }
    // Then those squares that can be selected to be open 
    // (white) paths are given the value of 2.  
    // We randomly select the square where the tree of maze 
    // paths will begin.  The maze is generated starting from 
    // this initial square and branches out from here in all 
    // directions to fill the maze grid.  
    Vector possibleSquares = new Vector(mySquares.length * mySquares[0].length);
    int[] startSquare = new int[2];
    startSquare[0] = getRandomInt(mySquares.length / 2) * 2 + 1;
    startSquare[1] = getRandomInt(mySquares[0].length / 2) * 2 + 1;
    mySquares[startSquare[0]][startSquare[1]] = 2;
    possibleSquares.addElement(startSquare);
    // Here we loop to select squares one by one to append to 
    // the maze pathway tree.
    while (possibleSquares.size() > 0) {
        // the next square to be joined on is selected randomly.
        int chosenIndex = getRandomInt(possibleSquares.size());
        int[] chosenSquare = (int[]) possibleSquares.elementAt(chosenIndex);
        // we set the chosen square to white and then 
        // remove it from the list of possibleSquares (i.e. squares 
        // that can possibly be added to the maze), and we link 
        // the new square to the maze.
        mySquares[chosenSquare[0]][chosenSquare[1]] = 1;
        possibleSquares.removeElementAt(chosenIndex);
        link(chosenSquare, possibleSquares);
    }
    // now that the maze has been completely generated, we 
    // throw away the objects that were created during the 
    // maze creation algorithm and reclaim the memory.
    possibleSquares = null;
    System.gc();
}