考察java基本代码的掌握
初始化时会遇到的空地址问题
初始化数组的基本格式
对于简单单链表的了解与掌握
区别于c、c++,没有指针
定义类方法中 this的使用
在不表明public 还是 private 还是 protect时,
java默认 private
基本单链表(含头节点)的操作
包括
遍历
查找
头插法
尾插法
额外要求,java中方法的参数个数可以是不确定的,了解该形式的方法编写
public class experiment15 {
public static void main(String[] args) {
MyLinkedList list = new MyLinkedList();
System.out.println(list);
list.addListHead(100);
list.addListTail(100);
System.out.println(list);
list = new MyLinkedList(1,2,3,4);
System.out.println(list);
}
}
class Node{
public int value;
public Node next;
public Node(){
};
public Node(int i) {
value = i;
next = null;
}
}
class MyLinkedList
{
private Node head;
public MyLinkedList()
{
Node newNode = new Node();
newNode.next = null;
head= newNode;
int a[] = new int[10];
for(int i = 0 ; i < 10; i++)
a[i] = (int)(Math.random()*100+1);
for(int i = 0 ; i < 10; i++)
this.addListTail(a[i]);
}
public MyLinkedList(int...args) {
Node newNode = new Node();
newNode.next = null;
head= newNode;
for(int i = 0 ; i < args.length; i++)
this.addListTail(args[i]);
}
public void addListHead(int d) {
Node newNode = new Node(d);//若链表只有头结点
if(head.next == null) {
head.next = newNode;
}
else {
newNode.next = head.next;//在head和head后的结点中插入新结点
head.next = newNode;
}
}
public void addListTail(int d) {
Node newNode = new Node(d);//若链表只有头结点
if(head.next == null) {
head.next = newNode;
}
else { //找到最后的结点赋值给 temp
Node temp = head.next;
while(temp.next!=null) {
temp = temp.next;
}
//将 新的结点插入
newNode.next = temp.next;// = null
temp.next = newNode;
}
}
public Node getHead()
{
return this.head;
}
public String toString() {
String s = "";
Node temp = this.getHead().next;
while(temp!=null) {
s += temp.value+"-->";
temp = temp.next;
}
return s;
}
}