public class HelloWorld {
public static void main(String []args) {
int[] arr = {18,13,50,15,4,17,18};
System.out.println("arr的排序前:\n18 13 50 15 4 17 18 ");
int temp = 0 ;
for(int i = 0 ;i< arr.length -1; i++){
for(int j = 0; j<arr.length-1-i; j++){
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("arr排序后:");
for(int i = 0; i<arr.length; i++){
System.out.print(arr[i]+"\t");
}
}
}
- 单例模式。
单例模式是一种常用的软件设计模式。
在它的核心结构中只包含一个被称为单例类的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。
- 工厂模式。
工厂模式主要是为创建对象提供了接口。
应用场景如下:
a、 在编码时不能预见需要创建哪种类的实例。
b、 系统不应依赖于产品类实例如何被创建、组合和表达的细节。
应用场景:如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。
- 策略模式。
策略模式:定义了算法族,分别封装起来,让它们之间可以互相替换。此模式让算法的变化独立于使用算法的客户。
应用场景如下。
a、 一件事情,有很多方案可以实现。
b、我可以在任何时候,决定采用哪一种实现。
c.、未来可能增加更多的方案。
d、 策略模式让方案的变化不会影响到使用方案的客户。
举例业务场景如下。
系统的操作都要有日志记录,通常会把日志记录在数据库里面,方便后续的管理,但是在记录日志到数据库的时候,可能会发生错误,比如暂时连不上数据库了,那就先记录在文件里面。日志写到数据库与文件中是两种算法,但调用方不关心,只负责写就是。
- 观察者模式。
观察者模式又被称作发布/订阅模式,定义了对象间一对多依赖,当一个对象改变状态时,它的所有依赖者都会收到通知并自动更新。
应用场景如下:
a、对一个对象状态的更新,需要其他对象同步更新,而且其他对象的数量动态可变。
b、对象仅需要将自己的更新通知给其他对象而不需要知道其他对象的细节。
public class MyLinkedList{
//内部类
class Node{
private int data;
private Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
private Node head;//可以理解为表头
public MyLinkedList() {
this.head = null;
}
//头插法
public void addFirst(int data) {
Node node=new Node(data);
if (this.head==null){
this.head=node;
}else{
node.next=this.head;
this.head=node;
}
}
//尾插法
public void addLast(int data) {
Node node=new Node(data);
Node cur=this.head;
if (this.head==null){
this.head=node;
}else{
while(cur.next!=null){
cur=cur.next;
}
cur.next=node;
}
}
//按照坐标进行插入
private Node search(int index){
checkIndex(index);
int count=0;
Node cur=this.head;
for (int i = 0; i <index-1 ; i++) {
cur=cur.next;
count++;
}
return cur;
}
private void checkIndex(int index){
if (index<0||index>getLength()){
throw new UnsupportedOperationException("位置不合法");
}
}
public boolean addIndex(int index, int data) {
if (index==0){
addFirst(data);
return true;
}
Node node=new Node(data);
Node cur=search(index);
node.next=cur.next;
cur.next=node;
return false;
}
//双向链表
import java.io.IOException;
import java.util.Scanner;
class Node{
public int data;//数据域
public Node next;//后继指针域
public Node previous;//前驱指针域
public Node(int data) {
this.data = data;
this.next = null;
this.previous = null;
}
//显示该结点的数据
public void display() {
System.out.print( data + " ");
}
}
public class Main {
public static Node head = null;//表头
public static int length = 0;
//在链表头部添加结点
public static void addHead(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;//如果链表为空,增加新结点
}
else {
newNode.next = head;
head.previous = newNode;
head = newNode;
}
length++;
}
//在链表头部删除结点
public static void deleteHead() {
if(head == null){
System.out.println("空表,删除的结点不存在");
}
else {
Node curNode = head;
head = curNode.next;
head.previous = null;
}
length--;
}
//在链表尾部添加结点
public static void addTail(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node curNode = head;
int count = 1;
while (count < length) {
curNode = curNode.next;
count++;
}
newNode.next = null;
newNode.previous = curNode;
curNode.next = newNode;
}
length++;
}
//在链表尾部删除结点
public static void deleteTail() {
if(head == null){
System.out.println("空表,删除的结点不存在");
}else{
Node preNode = head;
int count = 1;
while(count < length-1) {
preNode = preNode.next;
count++;
}
preNode.next = null;
}
length--;
}
//正向遍历链表
public static void printOrderNode() {
if(head == null) {
System.out.println("空表");
}
Node curNode = head;
while (curNode != null) {
curNode.display();
curNode = curNode.next;
}
System.out.println();
}
//反向遍历链表
public static void printReverseNode() {
if(head == null) {
System.out.println("空表");
}
Node curNode = head;
while (curNode.next != null) {
curNode = curNode.next;
}
while (curNode != null) {
curNode.display();
curNode = curNode.previous;
}
System.out.println();
}
//在指定位置插入结点
public static void insertList(int data, int index) {
Node newNode = new Node(data);
if(head == null){
head = newNode;//链表为空,插入
}
if(index > length+1 || index < 1) {
System.out.println("结点插入的位置不存在,可插入的位置为1到"+(length+1));
}
if(index == 1) {
newNode.next = head;
head.previous = newNode;
head = newNode;//在链表开头插入
} else{ //在链表中间或尾部插入
Node preNode = head;
int count = 1;
while(count < index-1) {
preNode = preNode.next;
count++;
}
Node curNode = preNode.next;
newNode.next = curNode;
newNode.previous = preNode;
preNode.next = newNode;
if(curNode != null) {
curNode.previous = newNode;
}
}
length++;
}
//在指定位置删除结点
public static void deleteList(int index) {
if(index > length || index < 1) {
System.out.println("结点删除的位置不存在,可删除的位置为1到"+length);
}
if(index == 1) {
Node curNode = head;
head = curNode.next;
head.previous = null;
length--;
} else{
Node preNode = head;
int count = 1;
while(count < index-1) {
preNode = preNode.next;
count++;
}
Node curNode = preNode.next;
Node laterNode = curNode.next;
preNode.next = laterNode;
if(laterNode != null) { //若被删除结点的后继结点不是null结点,那么设置其前驱结点
laterNode.previous = preNode;//指针指向被删除结点的前驱结点
}
length--;
}
}
//查找数据是否存在,与单链表一样
public static boolean containData(int data) {
if(head == null){
System.out.println("空表");
return false;
}
Node curNode = head;
while(curNode.data!= data){
if(curNode.next == null) {
System.out.println("结点数据不存在");
return false;
}
curNode =curNode.next;
}
System.out.println("结点数据存在");
return true;
}
//获取指定位置的数据,与单链表一样
public static void getIndexData(int index) {
if(head == null){
System.out.println("空表");
}
if(index > length || index < 1) {
System.out.println("结点位置不存在,可获取的位置为1到"+length);
}
Node curNode = head;
int count =1;
while(count != index) {
curNode =curNode.next;
count++;
}
curNode.display();
System.out.println();
}
//修改指定位置的结点数据,与单链表一样
public static void updateIndexData(int index, int data) {
if(head == null){
System.out.println("空表");
}
if(index > length || index < 1) {
System.out.println("结点位置不存在,可更新的位置为1到"+length);
}
Node curNode = head;
int count =1;//while也可以用for循环方式解决
while(count != index) {
curNode =curNode.next;
count++;
}
curNode.data = data;
}
//打印链表
public static void printList(){
Node tmp = head;
while(tmp != null){
System.out.print(tmp.data + " ");
tmp = tmp.next;
}
System.out.println();
}
public static void main(String []args) throws IOException {
Scanner sc = new Scanner(System.in);
while (true) {
int s = sc.nextInt();
if(s == 0) {
break;
}else {
addHead(s);
}
}
//输入样例
//1
//2
//3
//0 输入0结束
printList();
deleteHead();
printList();
printOrderNode();
Scanner in = new Scanner(System.in);
int index = sc.nextInt();
int data = sc.nextInt();
insertList(data, index);
printList();
}
}
已有表actor,且包含列last_name
//删除列
alter table actor drop last_name;
//增加列
alter table actor add last_name varchar(10);
//删除包含记录,包含某一个字符的
DELETE FROM actor WHERE last_name LIKE '%c%'
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
public class SingleObject {
//创建 SingleObject 的一个对象
private static SingleObject instance = new SingleObject();
//让构造函数为 private,这样该类就不会被实例化
private SingleObject(){}
//获取唯一可用的对象
public static SingleObject getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}
public class SingletonPatternDemo {
public static void main(String[] args) {
//不合法的构造函数
//编译时错误:构造函数 SingleObject() 是不可见的
//SingleObject object = new SingleObject();
//获取唯一可用的对象
SingleObject object = SingleObject.getInstance();
//显示消息
object.showMessage();
}
}