java数组删除数据_Java 数组 之 一维数组 删除 元素
/**
題目: 刪除scores數(shù)組索引index位置的值
思路:
1. 創(chuàng)建一個(gè)臨時(shí)比scores 小1的臨時(shí)數(shù)組tempArray
2. 將index前面的數(shù)據(jù)復(fù)制到tempArray前面
3. 將index后面的數(shù)組依次復(fù)制到 tempArray 后面
4. 將tempArray地址指針引用賦值給 scores
5. 打印輸出scores
*/
import java.util.Scanner;
public class ArrayDelete {
public static void main(String[] args) {
//一維數(shù)組的定義和初始化
int[] scores = { 90, 70, 50, 80, 60, 85 };
System.out.println("請(qǐng)輸入要?jiǎng)h除的索引index :");
Scanner in = new Scanner(System.in);
int index = in.nextInt();
//1. 創(chuàng)建一個(gè)臨時(shí)比scores 小1的臨時(shí)數(shù)組tempArray
int[] tempArray = new int[scores.length - 1];
//2. 將index前面的數(shù)據(jù)復(fù)制到tempArray前面
//3. 將index后面的數(shù)組依次復(fù)制到 tempArray 后面
for (int i = 0; i < scores.length; i++) {
if (i < index)
tempArray[i] = scores[i];
if (i > index)
tempArray[i - 1] = scores[i];
}
//4. 將tempArray地址指針引用賦值給 scores
scores = tempArray;
//5.打印輸出scores
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i] + ",");
}
}
}
總結(jié)
以上是生活随笔為你收集整理的java数组删除数据_Java 数组 之 一维数组 删除 元素的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java泛型集合_Java泛型集合(Se
- 下一篇: java美元兑换,(Java实现) 美元