File类的作用
能够读写文件的是数据流(OutputStream和InputStream)
内部类
静态内部类:
可以访问外部类的静态成员变量,包括私有变量,但是不能访问非晶态资源,可以不依赖外部类实例而实例化
成员内部类:
可以访问外部所有成员变量,但是自己不能定义静态资源,因为其实例化本身就依赖这外部类
局部内部类:
局部内部类,就像是一个内部方法,不能被访问修饰符修饰,也不能被static修饰,
只能访问所在代码块或者方法被定为为final的局部变量
匿名内部类:
没有类名的内部类,不能使用class,extends和implements,没有构造方法
不能定义静态资源,
匿名内部类是局部内部类的特殊形式,所以局部内部类的显示对匿名内部类有效
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public int[] exchange(int[] nums) {
int i = 0,j=nums.length-1;
int temp;
while(i<j){
while(i<j && nums[i]%2==1){
i++;
}
while(i<j && nums[j]%2==0){
j--;
}
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
return nums;
}
}
剑指 Offer 22. 链表中倒数第k个节点 - 力扣(LeetCode) (leetcode-cn.com)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
//快慢指针
ListNode slow = head,fast = head,per = null;
int i =0;
while(fast!=null){
if(i==k){
break;
}
fast = fast.next;
i++;
}
while(fast!=null){
fast = fast.next;
slow = slow.next;
}
return slow;
//方向看错了,不过就当复习反转链表了
// //反转链表
// //从slow开始到null;
// fast = slow;
// while(fast!=null){
// fast = fast.next;
// slow.next = per;
// per = slow;
// slow = fast;
// }
// return per;
}
}
剑指 Offer 24. 反转链表 - 力扣(LeetCode) (leetcode-cn.com)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
//哈哈哈,巧了,刚用上
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null,cur = head;
while(head!=null){
cur = head.next;
head.next = pre;
pre = head;
head = cur;
}
return pre;
}
}