Untitled
unknown
plain_text
a year ago
3.2 kB
11
Indexable
[26/02, 12:54 pm] +91 91282 20735: import java.util.*; public class vectordemo { public static void main(String arg[]) { Vector v=new Vector(); int x=10; Integer y=new Integer(20); String str="pk"; v.add(x); v.add(y); v.add(str); v.add(2,new Integer(30)); System.out.println("the elements of vector"+v); System.out.println("the size of vector are"+v.size()); System.out.println("the element at position 2 is"+v.elementAt(2)); System.out.println("the first element of the vector is"+v.firstElement()); v.removeElementAt(2); System.out.println("the elements of vector is "+v); System.out.println("the size of vector are"+v.size()); } } [26/02, 12:54 pm] +91 91282 20735: class ar3 { public static void main(String[] args) { char[] s = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' }; char[] d = new char[7]; System.arraycopy(s, 2, d, 0, 7); System.out.println(String.valueOf(d)); } } [26/02, 12:55 pm] +91 91282 20735: import java.util.Stack; class stk { public static void main(String[] args) { Stack<String> stk1= new Stack<>(); stk1.push("hii"); stk1.push("Amity"); stk1.push("university"); System.out.println("Stack: " + stk1); stk1.pop(); System.out.println("Stack after pop: " + stk1); } } [26/02, 12:57 pm] +91 91282 20735: // Stack implementation in Java class Stack { private int arr[]; // store elements of stack private int top; // represent top of stack private int capacity; //// total capacity of the stack // Creating a stack Stack(int size) { // initialize the array // initialize the stack variables arr = new int[size]; capacity = size; top = -1; } // push elements to the top of stack public void push(int x) { if (isFull()) { System.out.println("Stack OverFlow"); // terminates the program System.exit(1); } // insert element on top of stack System.out.println("Inserting " + x); arr[++top] = x; //top++; arr[top]=x; } // pop elements from top of stack public int pop() { // if stack is empty // no element to pop if (isEmpty()) { System.out.println("STACK EMPTY"); // terminates the program System.exit(1); } // pop element from top of stack return arr[top--]; } // return size of the stack public int getSize() { return top + 1; } // check if the stack is empty public Boolean isEmpty() { return top == -1; } // check if the stack is full public Boolean isFull() { return top == capacity - 1; } // display elements of stack public void printStack() { for (int i = 0; i <= top; i++) { System.out.print(arr[i] + ", "); } } public static void main(String[] args) { Stack stack = new Stack(5); stack.push(1); stack.push(2); stack.push(3); System.out.print("Stack: "); stack.printStack(); // remove element from stack stack.pop(); System.out.println("\nAfter popping out"); stack.printStack(); } }
Editor is loading...
Leave a Comment