链表
基本形式(依赖于引用传递和this调用当前对象)
class Node{
private String data; //要保存的数据
private Node next; //要保存的下一节点
public Node(String data){ //保存相对应的数据
this.data = data;
}
public void setNext(Node next){
this.next = next;
}
public Node getNext(){
return this.next;
}
public String getData(){
return this.data;
}
}
public class ListNode{
public static void main(String []args){
Node root = new Node("火车头");
Node n1 = new Node("车厢A");
Node n2 = new Node("车厢B");
root.setNext(n1);
n1.setNext(n2);
Node current = root;
while(current != null){
System.out.println(current.getData());
current = current.getNext();
}
}
}
装箱与拆箱
int与Integer
public class ListNode{
public static void main(String []args){
Integer obj = new Integer(10);
int temp = obj.intValue();
System.out.println(temp*2);
}
}
double与Double
public class ListNode{
public static void main(String []args){
Double obj = new Double(10.2);
Double temp = obj.doubleValue();
System.out.println(temp*2);
}
}
boolean与Boolean
public class ListNode{
public static void main(String []args){
Boolean obj = new Boolean(true);
Boolean temp = obj.booleanValue();
System.out.println(temp);
}
}
自动拆箱与装箱
public class ListNode{
public static void main(String []args){
Integer obj = 10; //自动装箱
int temp = obj; //自动拆箱
obj++;
System.out.println(temp*obj);
}
}
与String对象一致
public class ListNode{
public static void main(String []args){
Integer obja = 10; //自动装箱
Integer objb = 10;
Integer objc = new Integer(10);
System.out.println(obja == objc);
System.out.println(obja.equals(objc));
}
}
Object可以接收一切的引用数据类型
public class ListNode{
public static void main(String []args){
Object obj = 10;
int temp = (Integer)obj;
System.out.println(temp*2);
}
}
double必须给以指定的格式
public class ListNode{
public static void main(String []args){
Double obj = 10;
System.out.println(obj);
}
}
F:\prac>javac ListNode.java
ListNode.java:3: 错误: 不兼容的类型: int无法转换为Double
Double obj = 10;
^
1 个错误
---------------------------------------------------
public class ListNode{
public static void main(String []args){
Double obj = 10.2;
System.out.println(obj);
}
}
Boolean
public class ListNode{
public static void main(String []args){
Boolean flag = true;
if(flag){
System.out.println("Hello");
}
}
}
String与各个基本数据类型之间的转换操作
一、public static int parseInt(String s)
//被转换的数必须由数字组成
public class ListNode{
public static void main(String []args){
String str = "123";
int temp = Integer.parseInt(str);
System.out.println(temp*3);
}
}
二、public static double parseDouble(String s)
public class ListNode{
public static void main(String []args){
String str = "1.3";
double temp = Double.parseDouble(str);
System.out.println(temp*3);
}
}
三、public static boolean parseBoolean(String s)
//如果String的值不是true或false,则按false统一处理
public class ListNode{
public static void main(String []args){
String str = "true";
boolean temp = Boolean.parseBoolean(str);
if(temp){
System.out.println("满足条件");
}else{
System.out.println("不满足条件");
}
}
}
F:\prac>java ListNode
满足条件
---------------------------------------------------------
public class ListNode{
public static void main(String []args){
String str = "xfxhfdnxde";
boolean temp = Boolean.parseBoolean(str);
if(temp){
System.out.println("满足条件");
}else{
System.out.println("不满足条件");
}
}
}
F:\prac>java ListNode
不满足条件
String转基本数据类型
一、任何基本数据类型与String类型使用了“+”操作后均变为String类型
public class ListNode{
public static void main(String []args){
int num = 100;
String str = num+"";
System.out.println(str.replaceAll("0","9"));
}
}
二、public static String valueOf(数据类型 变量) public static String valueOf(int i)
public class ListNode{
public static void main(String []args){
int num = 100;
String str = String.valueOf(num);
System.out.println(str.replaceAll("0","9"));
}
}