Java Stack Push stackPushUnique(Stack s, Object val)

Here you can find the source of stackPushUnique(Stack s, Object val)

Description

Push a value into a stack if it is not already there

License

Creative Commons License

Declaration

public static void stackPushUnique(Stack s, Object val) 

Method Source Code


//package com.java2s;
//License from project: Creative Commons License 

import java.util.Stack;

public class Main {
    /** Push a value into a stack if it is not already there */
    public static void stackPushUnique(Stack s, Object val) {
        if (s == null) {
            return;
        }//from  www . j  a v  a2  s .c  o  m
        int pos = s.indexOf(val);
        if (pos == -1) {
            pos = s.size();
            s.push(val);
        }
    }
}

Related

  1. pushAll(Stack stack, Collection toBePushed)