Let me ask you a question: between an object of a base class and an object of a child class that is derived from the base class, which one is bigger in terms of information?
Answer is : derived class object. The reason is derived class object contains base class object. It's like a child gets all his/her parents' genes,but at meanwhile he/she gets his/her own stuff.
Saturday, December 7, 2013
Friday, December 6, 2013
a simple circular linked list in Java
class Node {
public int data;
public Node next;
}
class LinkList {
private Node head;
// LinkList constructor
public LinkList() { head = null; }
// Returns true if list is empty
public boolean isEmpty() { return head == null; }
// Inserts a new Node at the head of the list
public void insert(int a) {
Node node = new Node();
node.data = a;
node.next = null;
if (head == null)
{
head = node;
node.next = node; // a snake bites its tail.
}
else
{
Node last = getLastNode();
// insert node at the end of list.
last.next = node;
node.next = head; // bite head again.
}
}
// tops the Node at the head of the list
public void top() {
if (!isEmpty())
{
Node last = getLastNode();
if (head == last)
head=null;
else
{
head = head.next;
last.next = head;
}
}
else
System.out.println("empty!!!");
}
private Node getLastNode()
{
// Find the last node.
Node cursor = head;
Node last = head;
while (cursor != null)
{
if (cursor.next == head) { last = cursor; break;}
cursor = cursor.next;
}
return last;
}
// Prints list data
public void printList() {
Node cursor = head;
System.out.print("List: ");
while (cursor != null) {
System.out.print(cursor.data + " ");
cursor = cursor.next;
if (cursor == head) break;
}
System.out.println("");
}
public static void main(String[] args) {
LinkList list = new LinkList();
list.insert(1);
list.insert(2);
list.insert(3);
list.printList();
list.top();
list.top();
list.top();
list.top();
list.printList();
list.insert(3);
list.insert(2);
list.insert(3);
list.printList();
}
}
a simple double linked list in Java
class Node {
public int data;
public Node prev;
public Node next;
}
class LinkList {
private Node head;
// LinkList constructor
public LinkList() {
head = null;
}
// Returns true if list is empty
public boolean isEmpty() {
return head == null;
}
// Inserts a new Node at the head of the list
public void insert(int a) {
Node node = new Node();
node.data = a;
node.prev = null;
node.next = null;
if (head == null)
head = node;
else
{
// All below stuff is to find the last node.
Node cursor=head;
Node last = head;
while(cursor != null)
{
if (cursor.next == null) last = cursor;
cursor = cursor.next;
}
// Now insert new node at the end of list.
last.next = node;
node.prev = last;
}
}
// Top the node at the head of the list
public void top() {
head = head.next;
head.prev = null;
}
// Prints list data
public void printList() {
Node cursor = head;
System.out.print("List: ");
while (cursor != null) {
System.out.print(cursor.data + " ");
cursor = cursor.next;
}
System.out.println("");
}
public void printListBackwards() {
Node cursor=head;
Node last = head;
while(cursor != null)
{
// Find the last node.
if (cursor.next == null) last = cursor;
cursor = cursor.next;
}
cursor = last;
System.out.print("List: ");
while (cursor != null) {
System.out.print(cursor.data + " ");
cursor = cursor.prev;
}
System.out.println("");
}
public static void main(String[] args) {
LinkList list = new LinkList();
list.insert(1);
list.insert(2);
list.insert(3);
list.printList();
list.printListBackwards();
list.top();
list.printList();
list.printListBackwards();
}
}
a simple linked list in java
class Node {
public int data;
public Node next;
}
class LinkList {
private Node head;
// LinkList constructor
public LinkList() { head = null; }
// Returns true if list is empty
public boolean isEmpty() { return head == null; }
// Inserts a new Node at the head of the list
public void insert(int a) {
Node node = new Node();
node.data = a;
node.next = null;
if (head == null)
head = node;
else
{
// All stuff below is to find the last node.
Node cursor = head;
Node last = head;
while (cursor != null)
{
// Find the last node.
if (cursor.next == null)
last = cursor;
cursor = cursor.next;
}
// Now insert node at the end of list.
last.next = node;
}
}
// Top the node at the head of the list
public void top() { head = head.next; }
// Prints list data
public void printList() {
Node cursor = head;
System.out.print("List: ");
while (cursor != null) {
System.out.print(cursor.data + " ");
cursor = cursor.next;
}
System.out.println("");
}
public static void main(String[] args) {
LinkList list = new LinkList();
list.insert(1);
list.insert(2);
list.insert(3);
list.printList();
list.top();
list.top();
list.printList();
}
}
java inner class
1. Inner class are helper classes that can access member variables of something outer (class, function, blocks and expression ) and are for local use only. They are no need to be aware of by outside world.
2. Three types of inner classes:
inner class: inside a class
local class: inside any block ( expression, statement or block )
anonymous class: are expressions
Not matter how you categorize them, basically they are local to something outer.
I want to talk a little more about anonymous class. Basically it's 2-in-1: implementation + initialization
2. Three types of inner classes:
inner class: inside a class
local class: inside any block ( expression, statement or block )
anonymous class: are expressions
Not matter how you categorize them, basically they are local to something outer.
I want to talk a little more about anonymous class. Basically it's 2-in-1: implementation + initialization
interface HelloWorld {
public void greet();
public void greetSomeone(String someone);
}
HelloWorld frenchGreeting = new HelloWorld() {
String name = "tout le monde";
public void greet() {
greetSomeone("tout le monde");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};
It's the same as
class FrenchGreeting implements HelloWorld { same blah above...}
FrenchGreeting frenchGreeting = new FrenchGreeting();
However, by doing 2-in-1, you not only get rid of creating
new class name FrenchGreeting, which is why it's called anonymous
class, but also initialize an object of it. So it's much simpler.
java primitive type
| Data Type | Default Value (for fields) |
|---|---|
| byte 8-bit signed integer | 0 |
| short 16-bit signed integer | 0 |
| int 32-bit signed integer | 0 |
| long 64-bit singed integer | 0L |
| float 32-bit floating point | 0.0f |
| double 64-bit floating point | 0.0d |
| char 16-bit Unicode character | '\u0000' |
| boolean two possible values | false |
Literal: To describe something as literal is to say that it is exactly what it seems to be.
Literals ( = contants )
You may have noticed that thenew keyword isn't used
when initializing a variable of a primitive type. Primitive types are
special data types built into the language; they are not objects created
from a class. A literal is the source code representation of a
fixed value; literals are represented directly in your code without
requiring computation. As shown below, it's possible to assign a literal
to a variable of a primitive type:boolean result = true; char capitalC = 'C'; byte b = 100; short s = 10000; int i = 100000;
Monday, December 2, 2013
find with -exec or xargs
The most basic thing about find combined with -exec or xargs.
Basically, find finds a list of something and sends them to a command and let
the command to deal with them. The point I'm trying to make here is to show you that when find sends these something to a command, it can send them one at a time or all of them at a time. When it does one at a time, it's less efficient but it's safer and more flexible. In the opposite side, when it sends something all at a time, it's more efficient but less flexible.
Say you have three files: a1, a2 and a3
#1. \; sends parameters to the command following -exec one at a time.
$ find . -type f -name "a*" -exec echo '{}' \;
./a1 -- '{}' represents each of one of them each time echo runs.
./a2
./a3
#2. Both + in -exec and xargs: collects the filenames into groups or sets, and runs the command once per set
$ find . -type f -name "a*" -exec echo '{}' +
./a1 ./a2 ./a3
$ find . -type f -name "a*" | xargs
./a1 ./a2 ./a3
Summary:
1. -exec normally sends parameters found to command line one at a time with \; at end
2. -exec can also combine parameters into one line and send it as a whole to command line with + at end, similar to xargs does.
3. We can add extra parameters to command line when using -exec with \;. However, you are not able to do so using -exec using+, same goes to using xargs.The combined one line parameter is all you got to send to command line when using xargs or -exec with +.
4. -exec with + is kind of equal to xargs .
For example, ~/ is an extra parameter.
find . -type f -exec cp '{}' ~/ \;
find . -type f -exec cp '{}' ~/ + # not working
find . -type f | xargs cp ~/ # not working
Useful tips:
$ find . -type f -mmin -30 # files modified less than 30mins ago.
$ find . -type f -mtime -3 # files modified less than 3 days ago.
Basically, find finds a list of something and sends them to a command and let
the command to deal with them. The point I'm trying to make here is to show you that when find sends these something to a command, it can send them one at a time or all of them at a time. When it does one at a time, it's less efficient but it's safer and more flexible. In the opposite side, when it sends something all at a time, it's more efficient but less flexible.
Say you have three files: a1, a2 and a3
#1. \; sends parameters to the command following -exec one at a time.
$ find . -type f -name "a*" -exec echo '{}' \;
./a1 -- '{}' represents each of one of them each time echo runs.
./a2
./a3
#2. Both + in -exec and xargs: collects the filenames into groups or sets, and runs the command once per set
$ find . -type f -name "a*" -exec echo '{}' +
./a1 ./a2 ./a3
$ find . -type f -name "a*" | xargs
./a1 ./a2 ./a3
Summary:
1. -exec normally sends parameters found to command line one at a time with \; at end
2. -exec can also combine parameters into one line and send it as a whole to command line with + at end, similar to xargs does.
3. We can add extra parameters to command line when using -exec with \;. However, you are not able to do so using -exec using
4. -exec with + is kind of equal to xargs .
For example, ~/ is an extra parameter.
find . -type f -exec cp '{}' ~/ \;
find . -type f -exec cp '{}' ~/ + # not working
find . -type f | xargs cp ~/ # not working
Useful tips:
$ find . -type f -mmin -30 # files modified less than 30mins ago.
$ find . -type f -mtime -3 # files modified less than 3 days ago.
Subscribe to:
Posts (Atom)




