java-Array数组常用操作例子(基础必备)

时间:2024-07-05 12:34:32
package com.net.xinfang.reflect;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set; import org.apache.commons.lang.ArrayUtils; public class array001 {
/***
* 数组对象学习
*/
public void StrArray() {
String[] str = new String[] { "我", "是", "数组" };
// String[] str={ "我", "是", "数组" };
System.out.println(str.length);// 输出数组长度
boolean b = Arrays.asList(str).contains("我");// 转换为list再判断是否包含
if (b) {
System.out.println("exist");
}
// for遍历输出数组
for (int i = 0; i < str.length; i++) {
System.out.println(str[i]);
}
// foreach遍历输出数组
for (String j : str) {
System.out.println(j);
}
int arr[][] = new int[][] { { 1 }, { 2, 3 }, { 4, 5, 6 } }; // 二维数组
// for循环遍历二维数组。
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
// foreach语句遍历二维数组。
for (int x[] : arr) { // 外层遍历得到一维数组
for (int e : x) { // 内层遍历得到数组元素
System.out.print(e);
}
System.out.println();
}
// 连接两个数组
int[] arr1 = { 1, 2 };
int[] arr2 = { 3 };
int[] combArr = ArrayUtils.addAll(arr1, arr2);
for (int k : combArr) {
System.out.println(k);
}
// 数组逆向输出
int[] arra = { 1, 2, 3 };
ArrayUtils.reverse(arra);
System.out.println(Arrays.toString(arra));
// int数组转换为String
int[] array3 = { 9, 0 };
String arrStrings = Arrays.toString(array3);
System.out.println(arrStrings);
// 将数组转换ArrayList动态数组
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(str));
Set<String> set = new HashSet<String>(Arrays.asList(str));// 转换为set集合
System.out.println(set);
System.out.println(arrayList);
// 将数组转换为数组
String[] array2 = new String[] { "黄渤", "张艺兴", "孙红雷" };
// 方法 1.
List<String> list = new ArrayList<String>();
for (int i = 0; i < array2.length; i++) {
list.add(array2[i]);
}
System.out.println(list);
String[] arrStrings2 = { "1", "2", "3" };
// 方法 2.
List<String> list2 = java.util.Arrays.asList(arrStrings2);
System.out.println(list2);
// 填充数组
int[] arr3 = new int[2];
Arrays.fill(arr3, 10); // 将数组全部填充10
// 遍历输出
for (int i = 0; i < arr3.length; i++) {
System.out.println(arr3[i]);
}
// 数组排序
int[] arr4 = { 3, 7 };
Arrays.sort(arr4);
for (int i = 0; i < arr4.length; i++) {
System.out.println(arr4[i]);
}
// 比较两个数组
int[] arr9 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
boolean arr10 = Arrays.equals(arr4, arr9);
System.out.println(arr10);
// 数组去重
int[] arr11 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 3, 2, 4, 5, 6, 7, 4, 32, 2, 1, 1, 4, 6, 3 };
// 利用set的特性
Set<Integer> set2 = new HashSet<Integer>();
for (int i = 0; i < arr11.length; i++) {
set2.add(arr11[i]);
}
System.out.println(set2);
int[] arr12 = new int[set2.size()];
int j = 0;
for (Integer i : set2) {
arr12[j++] = i;
}
System.out.println(Arrays.toString(arr12));
// 求数组的最大值和最小值
int max = arr11[0];
for (int i = 1; i < arr11.length; i++) {
if (arr11[i] > max) {
max = arr11[i];
}
}
System.out.println("Max is " + max);
// 计算最小值
int min = arr11[0];
for (int i = 0; i < arr11.length; i++) {
if (arr11[i] < min) {
min = arr11[i];
}
}
System.out.println("Min is " + min);
} // 递归函数字符串反转
public String reverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return new StringBuilder(str).reverse().toString();
} // 倒序输出
public void reversa(String str) {
for (int i = str.length() - 1; i >= 0; i--) {
char c = str.charAt(i);
System.out.println(c);
}
} public String reverse3(String s) {
char[] array = s.toCharArray(); // 将string类型参数转化为char[]类型参数
String reverse = ""; // 注意这是空串,不是null
for (int i = array.length - 1; i >= 0; i--)
reverse += array[i];
return reverse;
} public String reverse2(String s) {
int length = s.length();
String reverse = "";
for (int i = 0; i < length; i++)
reverse = s.charAt(i) + reverse;// 字符串拼接先来排后面
return reverse;
} // 字符转换为数组,二分法
public void reversb(String str) {
char[] strArray = str.toCharArray();// 将字符串转换为数组
int len = strArray.length;
char temp;
for (int i = 0; i < len / 2; i++) { // 从中间向两边对换
temp = strArray[i];
strArray[i] = strArray[len - i - 1];
strArray[len - i - 1] = temp;
}
String strAfter = String.valueOf(strArray);// 数组转换为字符串
System.out.println(strAfter);
} // 选择排序(选择第一个与第二个比较,大的排后面,依次类推)
public void SelectSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
// 交换两个值
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
for (int k : a) {
System.out.print(k);
}
} // 冒泡排序(第1个与第2个比较 )
public void bubblSort(int a[]) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
for (int k : a) {
System.out.print(k);
}
} // 遍历判断查找
public int oneByoneSearch(int[] a, int value) {
for (int i = 0; i < a.length; i++) {
if (a[i] == value) {
return i;
}
}
return -1;
} // 二分查找
public int halfSearch2(int[] a, int value) {
int left = 0;
int right = a.length - 1;
int mid;
while (left <= right) {
mid = (left + right) >> 1;
if (a[mid] == value) {
return mid;
} else if (a[mid] > value) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
} public static void main(String args[]) {
array001 ay = new array001();
ay.StrArray();
String a = "abc";
System.out.println(ay.reverse(a));
ay.reversa(a);
ay.reversb(a);
ay.reverse3(a);
int b[] = { 1, 3, 5, 6 };
ay.SelectSort(b);
ay.halfSearch2(b, 3);
ay.bubblSort(b);
ay.oneByoneSearch(b, 5);
// 最简单的方法实现字符串反转
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String line = in.nextLine();// next()是遇到空格;nextLine()是遇到回车
StringBuffer sb = new StringBuffer(line);
sb.reverse();
System.out.println(sb.toString());
}
} }