数组反转
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
System.out.println("==反转后的数组情况==");
reverseArr(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static int[] reverseArr(int[] arr){
int temp = 0;
for (int i = 0; i < arr.length/2; i++) {
temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
return arr;
}
数组查找
根据数组下标查找数组元素
package com.test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
Scanner sc = new Scanner(System.in);
System.out.println("输入需要查找的数组元素下标 0~" + (arr.length - 1));
int find = sc.nextInt();
if (find <= arr.length){
System.out.println("找到了下标为" + find + " 数组元素为" + findArr(arr, find));
}else {
System.out.println("输入的数组元素下标不存在 退出程序");
}
}
public static int findArr(int[] arr, int find){
int element = 0;
for (int i = 0; i < arr.length; i++) {
if (find == i){
element = arr[i];
}
}
return element;
}
}
根据数组元素查找数组下标
package com.test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 4};
Scanner sc = new Scanner(System.in);
System.out.println("输入需要查找的数组元素值");
int find = sc.nextInt();
int index = -1;
for (int i = 0; i < arr.length; i++) {
if (find == arr[i]) {
System.out.println("找到了元素" + find + " 下标为" + i);
index = 0;
}
}
if (index == -1){
System.out.println("数组中没有该元素");
}
}
}
数组增加元素
package com.test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
Scanner sc = new Scanner(System.in);
System.out.println("输入需要添加的位置");
int inIndex = sc.nextInt();
System.out.println("输入需要添加的元素");
int inArr = sc.nextInt();
int[] newArr = new int[arr.length + 1];
if (inIndex >= newArr.length){
System.out.println("添加的数组下标越界\n默认将元素添加在数组最后");
inIndex = newArr.length-1;
}else if (inIndex < 0){
System.out.println("添加的数组下标越界\n默认将元素添加在数组第一位");
inIndex = 0;
}
for (int i = 0, j = 0; i < newArr.length; i++) {
if (inIndex == i){
newArr[i] = inArr;
}else {
newArr[i] = arr[j];
j++;
}
}
System.out.println("==添加元素后的数组情况==");
for (int i = 0; i < newArr.length; i++) {
System.out.print(newArr[i] + " ");
}
}
}
数组删除元素
根据数组下标删除数组元素
package com.test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
Scanner sc = new Scanner(System.in);
System.out.println("输入需要删除的位置");
int outIndex = sc.nextInt();
if (outIndex < 0){
System.out.println("输入的数组下标<0 默认删除第一位元素");
}else if (outIndex >= arr.length){
System.out.println("输入的数组下标>=数组长度 默认删除最后一位元素");
}
int[] newArr = new int[arr.length - 1];
for (int i = 0; i < newArr.length; i++) {
if (i >= outIndex){
newArr[i] = arr[i+1];
}else {
newArr[i] = arr[i];
}
}
System.out.println("==删除元素后的数组情况==");
for (int i = 0; i < newArr.length; i++) {
System.out.print(newArr[i] + " ");
}
}
}
根据数组元素删除数组元素
package com.test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
Scanner sc = new Scanner(System.in);
System.out.println("输入需要删除的元素");
int outArr = sc.nextInt();
int[] newArr = new int[arr.length - 1];
if (outArrIndex(arr, outArr) == -1){
System.out.println("输入的元素在数组中找不到 自动退出");
return;
}else {
printArr(assignmentArr(arr, newArr, outArrIndex(arr, outArr)));
}
}
public static int outArrIndex(int[] arr, int outArr){
int outArrIndex = -1;
for (int i = 0; i < arr.length; i++) {
if (outArr == arr[i]){
outArrIndex = i;
}
}
return outArrIndex;
}
public static int[] assignmentArr(int[] arr, int[] newArr, int outArrIndex){
for (int i = 0; i < newArr.length; i++) {
if (i >= outArrIndex){
newArr[i] = arr[i+1];
}else {
newArr[i] = arr[i];
}
}
return newArr;
}
public static void printArr(int[] newArr){
System.out.println("==删除元素后的数组情况==");
for (int i = 0; i < newArr.length; i++) {
System.out.print(newArr[i] + " ");
}
}
}