Java and C Sharp

Everyone knows java and c# are quite similar, from basic syntax to garbage collection mechanism. A skilled java developer can easily transfer to a c# developer and vice versa. Now in this post I would like collection information and compare the two programming languages.

垃圾回收

####new关键字

在c#编程中,使用new进行实例化,自动分配内存,但是不用考虑使用完成后去delete掉,分配内存也不用像c++在堆中malloc一段地址。
在java和c#中都是自动管理栈和堆,直接操作内存熟练之后确实也很方便又意思,但是对于一些需要简单快速开发的程序来说,c#的垃圾回收机制确实使其成为一个很方便的语言。
好处也是显而易见的,我的理解主要在于这么两个方面:首先,GC机制使得没有内存泄漏,不再需要的时候自动dispose掉object。然后GC机制更有利于面相对象编程,直接调用构造函数即可。

语法区别

链表

链表是最基础的数据结构之一,简单可以理解为自行车上的车链,一个纽扣下一个扣,一个Node连着下一个Node。
相对于数组的好处是,方便扩充,不用提前分配内存。
链表的节点:链表的节点(Node)起码包含两个元素,键值和下个一个节点(Node)。

1
2
3
4
5
struct LinkedList_Node
{
int m_key;
LinkedList_Node* pNextNode;
}

链表的生成

由于链表是一环扣一环的,所以首节点即可表示整个链表。下面是如何生成一个链表(c++),以节点为10的链表为例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void CreateLinkedList(Linkedlist_Node* pHeadNode)
{

LinkedList_Node* p = pHeadnode;
for (int i=1; i<10; i++) //创建另外9个节点
{
ListNode* pNewNode = new LinkedList_Node;
pNewNode -> m_key = i;
pNewNode -> pNextNode = NULL;
p -> pNextNode = pNewNode;
p = pNewNode;
}
}

int main()
{

LinkedList_Node* head = NULL;
head = new LinkedList_Node; //c++中struct的初始化不用加(),毕竟不是class
head -> m_key =0; //初始化首节点
head -> pNextNode = NULL;
CreateLinkedList(head);

return 0;
}

以下链表的java实现,链表的节点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.xtfggef.algo.linkedlist;

public class LinkedList<T> {

/**
* class node
* @author wanghuan
* @param <T>
*/

private static class Node<T> {
T data;
Node<T> next;

Node(T data, Node<T> next) {
this.data = data;
this.next = next;
}

Node(T data) {
this(data, null);
}
}

// data
private Node<T> head, tail;

public LinkedList() {
head = tail = null;
}

/**
* judge the list is empty
*/

public boolean isEmpty() {
return head == null;
}

/**
* add head node
*/

public void addHead(T item) {
head = new Node<T>(item);
if (tail == null)
tail = head;
}

/**
* add the tail pointer
*/

public void addTail(T item) {
if (!isEmpty()) {
tail.next = new Node<T>(item);
tail = tail.next;
} else {
head = tail = new Node<T>(item);
}
}

/**
* print the list
*/

public void traverse() {
if (isEmpty()) {
System.out.println("null");
} else {
for (Node<T> p = head; p != null; p = p.next)
System.out.println(p.data);
}
}

/**
* insert node from head
*/

public void addFromHead(T item) {
Node<T> newNode = new Node<T>(item);
newNode.next = head;
head = newNode;
}

/**
* insert node from tail
*/

public void addFromTail(T item) {
Node<T> newNode = new Node<T>(item);
Node<T> p = head;
while (p.next != null)
p = p.next;
p.next = newNode;
newNode.next = null;
}

/**
* delete node from head
*/

public void removeFromHead() {
if (!isEmpty())
head = head.next;
else
System.out.println("The list have been emptied!");
}

/**
* delete frem tail, lower effect
*/

public void removeFromTail() {
Node<T> prev = null, curr = head;
while (curr.next != null) {
prev = curr;
curr = curr.next;
if (curr.next == null)
prev.next = null;
}
}

/**
* insert a new node
* @param appointedItem
* @param item
* @return
*/

public boolean insert(T appointedItem, T item) {
Node<T> prev = head, curr = head.next, newNode;
newNode = new Node<T>(item);
if (!isEmpty()) {
while ((curr != null) && (!appointedItem.equals(curr.data))) {
prev = curr;
curr = curr.next;
}
newNode.next = curr;
prev.next = newNode;
return true;
}
return false;
}

public void remove(T item) {
Node<T> curr = head, prev = null;
boolean found = false;
while (curr != null && !found) {
if (item.equals(curr.data)) {
if (prev == null)
removeFromHead();
else
prev.next = curr.next;
found = true;
} else {
prev = curr;
curr = curr.next;
}
}
}


public int indexOf(T item) {
int index = 0;
Node<T> p;
for (p = head; p != null; p = p.next) {
if (item.equals(p.data))
return index;
index++;

}
return -1;
}

/**
* judge the list contains one data
*/

public boolean contains(T item) {
return indexOf(item) != -1;
}
}

链表的删除

删除链表,遍历一下这个链表,使用delete删除即可。c++中所有new初始化的实例,都使用delete来删除。释放内存。

复杂度

对于有些场景下,复杂度还是非常有必要去计算的,这里回顾一下算法的复杂度。今天的一场世界冠军李世石对阵谷歌AI的围棋大战倍受瞩目,在优秀算法的支持之下,
人工智能的优势是碾压性的。即使像围棋这样尚未完全开发出可行算法战胜人脑的,也通过mega级别的计算能力和分析大量存储的棋谱取得优势。

复习一下原来评价算法的基础,复杂度。复杂度可分为时间复杂度和空间复杂度。
Here I would like review the basic of algorithm complexity. We use complexity to evaluate the efficiency of algorithm in terms of time complexity and space complexity.

时间复杂度

空间复杂度

Sorting Algorigms

Finally I get time to relax and note something. Now I will review several basic algorithms and take it as a chance for reviewing c++ programming. All demenstration codes will be shown in c++.

Bubble sort

Bubble sort has the top priority in the sorting algorithms and it often occurs in the interview or examination(Perhaps the philosophy that is pretty simple).So we get start from bubble sort:

1
2
3
4
5
6
7
8
9
10
11
int arr[]= {5, 1, 2, 7, 4, 3, 6};
int length = (int) sizeof(arr)/sizeof(*arr); // get the length of array
for (int i = 0; i < length - 1; i++)
for (int j = 0; j < length - 1 - i; j++)
if (arr[j] > arr[j+ 1])
swap(arr[j],arr[j+1]);

for (i = 0; i < length; i++)
std::cout << arr[i] << ' ';
std::cout << std::endl;
return 0;

The basic idea of bubble sorting is comparing every neighboring two elements, thus the largest or smallest one will be arranged to the last one after a round. Then repeat this process until all elements in order.

Quicksort

Quick sort is a fast and efficient sorting algorithm.

静态网站

配置好了blog,作一个简单的记录,包括配置博客的过程和静态网站生成器的使用。借助于各种技术的支持,搭建一个网站变得非常的简单。

服务器

github page可以直接把github里的repository搭建为静态网站,并附带详细的搭建步骤。

博客框架

如果不想手写html文件,可以使用一些静态网站生成器自动生成文件,常用的有jekyll等,这里使用的是博客框架hexo。这是一个非常简答,易于使用的框架,基于node.jsgit

hexo常用使用指令:

  • 写一个新的日志:
$ hexo new "A NEW BLOG"
  • 生成静态网站,自动生成并反馈改动:
$ hexo generate 
  • 部署到github page:
$ hexo deploy

Visual Studio Code

对于web前段设计者和.net程序员来说,尤其像对于vim接触比较少,Visual Studio Code也是一个很好用的文档编辑器。

Mac Terminal中打开vs code,需要在配置bash_profile中加入:

code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $*; }

修改bash_profile用editer nano即可:

$ nano ~/.bash_profile

修改之后,命令code可用来打开或者新建,用法和vim一样:

  • code – 打开 Visual Studio Code
  • code . – 在 Visual Studio Code 打开当前文件
  • code somefile – 在 Visual Studio Code 中打开某一文件

This is my first approach for static blog, drived by Hexo and hosted on the github page.

In this blog, I will record the experience and my thanking in the work. What I am facing is having too much theoretical knowledge but being lack of practice and project experience.

博客语言为汉语和英语,可能会混杂,以后看情况可能逐渐只用英语写。这是一个记录,也是一个对自我的督查。