package com.app.Problem.problem0715;
/**
* Explain:对数组的增删查找操作
* @author Administrator
*
*/
public class Problem_04 {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int[] prray = addNumber(array, 3, 6);
int[] crray = deleteNum(array, 3);
int index = searchNum(array, 9);
ptintArray(array);
ptintArray(prray);
ptintArray(crray);
System.out.println(index);
}
/**
* 向数组中添加元素
* @param array
* @param i
* @param number
* @return
*/
static int[] addNumber(int[] array, int i, int number) {
int[] prray = new int[array.length + 1];
for (int j = 0; j < prray.length; j++) {
if(j<i)
{
prray[j]=array[j];
}
if (j == i) {
prray[j] = number;
} else if(j>i){
prray[j] = array[j - 1];
}
}
return prray;
}
/**
* 删除数组中的元素
* @param array
* @param i
* @return
*/
static int[] deleteNum(int[] array, int i) {
int[] prray = new int[array.length - 1];
for (int j = 0; j < array.length; j++) {
if (j < i) {
prray[j] = array[j];
} else if (j > i) {
prray[j - 1] = array[j];
}
}
return prray;
}
/**
* 查找数组元素
* @param array
* @param number
* @return
*/
static int searchNum(int[] array, int number) {
int index = 0;
for (int i = 0; i < array.length; i++) {
if (number == array[i]) {
index = i;
}
}
return index;
}
static void ptintArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}
/**
* Explain:对数组的增删查找操作
* @author Administrator
*
*/
public class Problem_04 {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int[] prray = addNumber(array, 3, 6);
int[] crray = deleteNum(array, 3);
int index = searchNum(array, 9);
ptintArray(array);
ptintArray(prray);
ptintArray(crray);
System.out.println(index);
}
/**
* 向数组中添加元素
* @param array
* @param i
* @param number
* @return
*/
static int[] addNumber(int[] array, int i, int number) {
int[] prray = new int[array.length + 1];
for (int j = 0; j < prray.length; j++) {
if(j<i)
{
prray[j]=array[j];
}
if (j == i) {
prray[j] = number;
} else if(j>i){
prray[j] = array[j - 1];
}
}
return prray;
}
/**
* 删除数组中的元素
* @param array
* @param i
* @return
*/
static int[] deleteNum(int[] array, int i) {
int[] prray = new int[array.length - 1];
for (int j = 0; j < array.length; j++) {
if (j < i) {
prray[j] = array[j];
} else if (j > i) {
prray[j - 1] = array[j];
}
}
return prray;
}
/**
* 查找数组元素
* @param array
* @param number
* @return
*/
static int searchNum(int[] array, int number) {
int index = 0;
for (int i = 0; i < array.length; i++) {
if (number == array[i]) {
index = i;
}
}
return index;
}
static void ptintArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
}