自主数据库模型
排序:package com.huawei;
public class Main {
public static void main(String[] args) {
int[] a = new int[10];
a = new int[]{12, 21, 34, 32, 2, 12, 56, 31, 98, 65, 46};
int[] b = new int[10];
int len = a.length;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (a[j] > a[j + 1]) { // 相邻元素两两对比
int temp = a[j + 1]; // 元素交换
a[j + 1] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}
}
}
去重:package com.huawei;
import java.util.Random;
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] ran1 = new int[N];
for (int i = 0; i < N; i++) {
ran1[i] = sc.nextInt();
}
for (int m = 1; m < N; m++) {
for (int n = N - m; n < N; n++) {
if (ran1[n - 1] > ran1[n]) {
int temp = ran1[n - 1];
ran1[n - 1] = ran1[n];
ran1[n] = temp;
}
}
}
int count[] = new int[62];
int count12[][] = new int[62][62];
String chen = "";
for (int m = 0; m < 62; m++) {
count[m] = 0;
}
for (int i = 0; i < ran1.length-1; i++) {
if (ran1[i] == ran1[i+1])
for (int j = i; j < ran1.length-1; j++) {
ran1[j] = ran1[j+1];
}
}
for (int i = 0; i < N-3; i++) {
System.out.println(ran1[i]);
}
}
}
查找:
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Stream;
import static java.lang.String.format;
public class IterativeBinarySearch {
/**
* This method implements an iterative version of binary search algorithm
*
* @param array a sorted array
* @param key the key to search in array
* @return the index of key in the array or -1 if not found
*/
public <T extends Comparable> int find(T[] array, T key) {
int l, r, k, cmp;
l = 0;
r = array.length - 1;
while (l <= r) {
k = (l + r) / 2;
cmp = key.compareTo(array[k]);
if (cmp == 0) {
return k;
} else if (cmp < 0) {
r = --k;
} else {
l = ++k;
}
}
return -1;
}
//Only a main method for test purpose
public static void main(String[] args) {
Random r = new Random();
int size = 100;
int maxElement = 100000;
Integer[] integers = Stream.generate(() -> r.nextInt(maxElement)).limit(size).sorted().toArray(Integer[]::new);
//the element that should be found
Integer shouldBeFound = integers[r.nextInt(size - 1)];
IterativeBinarySearch search = new IterativeBinarySearch();
int atIndex = search.find(integers, shouldBeFound);
System.out.println(String.format("Should be found: %d. Found %d at index %d. An array length %d"
, shouldBeFound, integers[atIndex], atIndex, size));
int toCheck = Arrays.binarySearch(integers, shouldBeFound);
System.out.println(format("Found by system method at an index: %d. Is equal: %b", toCheck, toCheck == atIndex));
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class DatabaseInsert {
public static void main(String []args)
{
Map<Integer,String> database = new HashMap<Integer,String>();
System.out.println(“请输入数据条编号和字符串型数据”);
Scanner sc = new Scanner(System.in);
int index = sc.nextInt();
String s = sc.nextLine();
database.put(index, s);
}
}