Java – Deque Example

Output:

[Element 6 (Head), Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)]

Standard Iterator
Element 6 (Head)
Element 4 (Head)
Element 2 (Head)
Element 1 (Tail)
Element 3 (Tail)
Element 5 (Tail)
Element 7 (Tail)
Reverse Iterator
Element 7 (Tail)
Element 5 (Tail)
Element 3 (Tail)
Element 1 (Tail)
Element 2 (Head)
Element 4 (Head)
Element 6 (Head)
Peek Element 6 (Head)
After peek: [Element 6 (Head), Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)]
Pop Element 6 (Head)
After pop: [Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)]
Contains element 3: true

Java JSP – Using Abstract Class

[code lang=”html”]<HTML>
<HEAD>
<TITLE>Using Abstract Classes</TITLE>
</HEAD>

<BODY>
<H1>Using Abstract Classes</H1>

<%!
javax.servlet.jsp.JspWriter localOut;

abstract class a {
abstract String getText() throws java.io.IOException;

public void printem() throws java.io.IOException {
localOut.println(getText());
}
}

class b extends a {
String getText() throws java.io.IOException {
return "Hello from JSP!";
}
}
%>
<%
localOut = out;

b bObject = new b();

bObject.printem();
%>
</BODY>
</HTML>[/code]

Java – Bubble Sort Array Example

[code lang=”java”]import java.util.Arrays;

/**
* Algorithm
*
* 1] Compare each pair of adjacent elements from beginning
* of an array and, if they are in reversed order, swap them.
*
* 2] If at least one swap has been done, repeat step 1.
*
*/
public class MyBubbleSort {

private void sortData(int[] data) {
boolean swapDone = false;
do {
swapDone = false;
for (int i = 0; i < data.length-1; i++) {
if (data[i] > data[i+1]) {
swapDone = true;
int tmp = data[i];
data[i] = data[i+1];
data[i+1] = tmp;
}
}
} while (swapDone == true);
}

public static void main(String[] args) {
int[] data = {5,1,12,-5,16};
System.out.println(Arrays.toString(data));

new MyBubbleSort().sortData(data);
System.out.println(Arrays.toString(data));
}

}[/code]