Java常用算法函数实现
发布时间:2023-06-23 04:08:12
Java是一种高级编程语言,使用广泛。在Java编程中,算法是基础和重要的部分。Java中有许多常用算法函数,这些函数可以帮助程序员快速编写代码,实现一些常见操作,如排序、查找、加密等。本文将介绍几个常用的Java算法函数实现。
1. 排序算法
排序算法是程序员经常使用的算法之一,Java中提供了Arrays类和Collections类实现常用的排序算法,包括插入排序、冒泡排序、归并排序、快速排序等。
示例:
int[]arr = {10, 7, 8, 9, 1, 5};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
输出结果: [1, 5, 7, 8, 9, 10]
2. 查找算法
查找算法是在给定数据集中寻找特定元素的算法。Java中提供了常见的查找算法,如线性查找、二分查找等。
示例:
import java.util.Scanner;
public class BinarySearchExample {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,7,8,9,10};
Scanner in=new Scanner(System.in);
System.out.println("Enter the element to be searched:");
int key=in.nextInt();
int result = binarySearch(arr, key);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
}
static int binarySearch(int arr[], int key) {
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key)
return mid;
if (arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}
}
3. 加密算法
加密算法是保护数据安全的重要手段之一。Java中提供了许多加密算法函数,如DES、AES等。
示例:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
public class DESEncryption {
private static final String ALGO = "AES";
private static final byte[] keyValue =
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new
Base64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new
Base64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
public static void main(String[] args) throws Exception {
String password = "password";
String passwordEnc = DESEncryption.encrypt(password);
String passwordDec = DESEncryption.decrypt(passwordEnc);
System.out.println("Plain Text : " + password);
System.out.println("Encrypted Text : " + passwordEnc);
System.out.println("Decrypted Text : " + passwordDec);
}
}
4. 图像处理算法
Java提供了许多函数用于图像处理,如图像读取、图像缩放、图像滤波等。
示例:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ImageFilter {
public static void main(String[] args) throws Exception {
File originalImage = new File("input.jpg");
BufferedImage img = ImageIO.read(originalImage);
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
Color c = new Color(img.getRGB(i, j));
int red = (int) (c.getRed() * 0.299);
int green = (int) (c.getGreen() * 0.587);
int blue = (int) (c.getBlue() * 0.114);
Color newColor = new Color(red + green + blue,
red + green + blue, red + green + blue);
img.setRGB(i, j, newColor.getRGB());
}
}
File newFile = new File("output.jpg");
ImageIO.write(img, "jpg", newFile);
}
}
以上是Java中常用的几个算法函数实现。通过熟练掌握这些函数,程序员可以更快地编写代码,提高开发效率,也可以更好地保护数据安全。
