java中最常用的就是数组了,关于数组的用法,总结如下:
/* 声明数组; 初始化数组; 查看数组长度; 遍历数组; int数组转成string数组; 从array中创建arraylist; 数组中是否包含某一个值; 将数组转成set集合; 将数组转成list集合; Arrays.fill()填充数组; 数组排序; 复制数组; 比较两个数组; 去重复; 查询数组中的最大值和最小值;
(1) 声明数组
int[]arr1;
String []arr2;
String[]arr4 =
new String[
10];
(2)初始化数组
int []arr3 =
new int[]{
1,
2,
3};
String[] arr5 =
new String[] {
"自信",
"充实",
"喜乐"};
for (
int i =
0; i < arr3.length; i++) {
arr3[i] =i+
1;
}
(3)查看数组长度;
int length = arr4.length;
System.out.println(length);
4、遍历数组;
for (
int i =
0; i < arr5.length; i++) {
System.out.print(arr5[i]);
}
5、int数组转成string数组
String arr6=Arrays.toString(arr3);
System.out.println(arr6);
6、从array中创建arraylist
ArrayList<String> arrlist=
new ArrayList<String>(Arrays.asList(arr5));
System.out.println(arrlist);
7、数组中是否包含某一个值
String a =
"自信";
if (Arrays.asList(arr5).contains(a)) {
System.out.print(
"言你要自信,不要拿自己之短比别人之长");
}
8、将数组转成set集合
Set<String> myset=
new HashSet<String>(Arrays.asList(arr5));
System.out.println(myset);
9、将数组转成list集合
List<String > list2=java.util.Arrays.asList(arr5);
System.out.println(list2);
10、Arrays.fill()填充数组
int []arr7=
new int[
5];
Arrays.fill(arr7,
10);
for (
int i =
0; i < arr7.length; i++) {
System.out.print(arr7[i]);
}
11、数组排序
int []arr8=
new int[]{
4,
7,
2,
5,
1};
Arrays.sort(arr8);
for (
int i =
0; i < arr8.length; i++) {
System.out.println(arr8[i]);
}
int []arr9=
new int[]{
4,
7,
2,
5,
1,
13,
17};
Arrays.sort(arr9,
2,
6);
for (
int i =
0; i < arr9.length; i++) {
System.out.println(arr9[i]);
}
12、复制数组
int []arr10 =Arrays.copyOf(arr9,
10);
int []arr11= Arrays.copyOfRange(arr9,
2,
6);
for (
int i =
0; i < arr11.length; i++) {
System.out.println(arr11[i]);
}
13、比较两个数组
boolean b =Arrays.equals(arr10, arr11);
System.out.print(b);
14、去重复
int[] arr12 = {
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<Integer> set1=
new HashSet<Integer>() ;
for (
int i =
0; i < arr12.length; i++) {
set1.add(arr12[i]);
System.out.println(set1);
int arr13[] =
new int[set1.size()];
int j=
0;
for(Integer k:set1){
arr13[j++]=i;
}
System.out.println(Arrays.toString(arr13));
}
15、查询数组中的最大值和最小值
int max=arr12[
0];
for (
int i =
0; i < arr12.length; i++) {
if (arr12[i]>max) {
max=arr12[i];
}
}
System.out.println(max);
int min =arr12[
0];
for (
int i =
0; i < arr12.length; i++) {
if (arr12[i]<max) {
min=arr12[i];
}
}
System.out.println(min);
}
完整的代码:
package zyy.pack;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.omg.CORBA.PUBLIC_MEMBER;
public class array {
public static void main(String args[]){
int[]arr1;
String []arr2;
String[]arr4 =
new String[
10];
int []arr3 =
new int[]{
1,
2,
3};
String[] arr5 =
new String[] {
"自信",
"充实",
"喜乐"};
for (
int i =
0; i < arr3.length; i++) {
arr3[i] =i+
1;
}
int length = arr4.length;
System.
out.println(length);
for (
int i =
0; i < arr5.length; i++) {
System.
out.println(arr5[i]);
}
String arr6=Arrays.toString(arr3);
System.
out.println(
"arr6 是"+arr6);
ArrayList<String> arrlist=
new ArrayList<String>(Arrays.asList(arr5));
System.
out.println(arrlist);
String a =
"自信";
if (Arrays.asList(arr5).contains(a)) {
System.
out.println(
"言言要自信,不要拿自己之短比别人之长");
}
Set<String> myset=
new HashSet<String>(Arrays.asList(arr5));
System.
out.println(
"myset 是"+myset);
List<String > list2=java.util.Arrays.asList(arr5);
System.
out.println(list2);
int []arr7=
new int[
5];
Arrays.fill(arr7,
5);
for (
int i =
0; i < arr7.length; i++) {
System.
out.println(
"填充数组"+arr7[i]);
}
int []arr8=
new int[]{
4,
7,
2,
5,
1};
Arrays.sort(arr8);
for (
int i =
0; i < arr8.length; i++) {
System.
out.print(arr8[i]);
}
int []arr9=
new int[]{
4,
7,
2,
5,
1,
13,
17};
Arrays.sort(arr9,
2,
6);
for (
int i =
0; i < arr9.length; i++) {
System.
out.print(arr9[i]);
}
int []arr10 =Arrays.copyOf(arr9,
10);
int []arr11= Arrays.copyOfRange(arr9,
2,
6);
for (
int i =
0; i < arr11.length; i++) {
System.
out.println(arr11[i]);
}
boolean b =Arrays.equals(arr10, arr11);
System.
out.println(
"这两个数组是否相等"+b);
int[] arr12 = {
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<Integer> set1=
new HashSet<Integer>() ;
for (
int i =
0; i < arr12.length; i++) {
set1.add(arr12[i]);
}
System.
out.println(
"这里是set1的元素"+set1);
int arr13[] =
new int[set1.size()];
int j=
0;
for(Integer k:set1){
arr13[j++]=k;
}
System.
out.println(
"这里是set1转成数组后的元素"+Arrays.toString(arr13));
int max=arr12[
0];
for (
int i =
0; i < arr12.length; i++) {
if (arr12[i]>max) {
max=arr12[i];
}
}
System.
out.println(max);
int min =arr12[
0];
for (
int i =
0; i < arr12.length; i++) {
if (arr12[i]<min) {
min=arr12[i];
}
}
System.
out.println(min);
}
}
输出结果
10 自信 充实 喜乐 arr6 是[1, 2, 3] [自信, 充实, 喜乐] 你言要自信,不要拿自己之短比别人之长 myset 是[充实, 自信, 喜乐] [自信, 充实, 喜乐] 填充数组5 填充数组5 填充数组5 填充数组5 填充数组5 124574712513171 2 5 13 这两个数组是否相等false 这里是set1的元素[0, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9] 这里是set1转成数组后的元素[0, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9] 32 0 原文链接https://www.cnblogs.com/epeter/p/5664926.html