java如何删除数组的元素??
比如,inta={1,2,3,4,5};4我不想要了,就是数组变成inta={1,2,3,5};怎么写??3Q!...
比如,int a = { 1,2,3,4,5};
4我不想要了,就是数组变成int a = { 1,2,3,5};
怎么写?? 3Q! 展开
4我不想要了,就是数组变成int a = { 1,2,3,5};
怎么写?? 3Q! 展开
展开全部
楼主你好
具体代码如下:
public class Test {
private int a[] = {1,2,3,4,5};//数组初始值1 2 3 4 5
public void delete(int n)//删除数组中n的值
{
for (int i = 0; i < a.length; i++) {
if(a[i] == n)
{
for(int j = i; j < a.length-1; j++)
{
a[j] = a[j+1];
}
}
}
}
public void print()//打印数组
{
for (int i = 0; i < a.length-1; i++) {
System.out.println (a[i]);
}
}
public static void main(String[] args) {
Test t = new Test();
t.delete(4);
t.print();
}
}
运行结果:
1
2
3
5
希望能帮助你哈
具体代码如下:
public class Test {
private int a[] = {1,2,3,4,5};//数组初始值1 2 3 4 5
public void delete(int n)//删除数组中n的值
{
for (int i = 0; i < a.length; i++) {
if(a[i] == n)
{
for(int j = i; j < a.length-1; j++)
{
a[j] = a[j+1];
}
}
}
}
public void print()//打印数组
{
for (int i = 0; i < a.length-1; i++) {
System.out.println (a[i]);
}
}
public static void main(String[] args) {
Test t = new Test();
t.delete(4);
t.print();
}
}
运行结果:
1
2
3
5
希望能帮助你哈
展开全部
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class T {
public static void main(String[] args) {
int[] array = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(array));
array = deleteByValue(array, 4);
System.out.println(Arrays.toString(array));
array = deleteByIndex(array, 0);
System.out.println(Arrays.toString(array));
}
public static int[] deleteByIndex(int[] array, int index) {
int[] newArray = new int[array.length - 1];
for (int i = 0; i < newArray.length; i++) {
if (i < index) {
newArray[i] = array[i];
} else {
newArray[i] = array[i + 1];
}
}
return newArray;
}
public static int[] deleteByValue(int[] array, int value) {
List<Integer> list = new ArrayList();
for (int i : array) {
if (i != value) {
list.add(i);
}
}
int[] newArray = new int[list.size()];
for (int i = 0; i < newArray.length; i++) {
newArray[i] = list.get(i);
}
return newArray;
}
}
import java.util.Arrays;
import java.util.List;
public class T {
public static void main(String[] args) {
int[] array = new int[] { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(array));
array = deleteByValue(array, 4);
System.out.println(Arrays.toString(array));
array = deleteByIndex(array, 0);
System.out.println(Arrays.toString(array));
}
public static int[] deleteByIndex(int[] array, int index) {
int[] newArray = new int[array.length - 1];
for (int i = 0; i < newArray.length; i++) {
if (i < index) {
newArray[i] = array[i];
} else {
newArray[i] = array[i + 1];
}
}
return newArray;
}
public static int[] deleteByValue(int[] array, int value) {
List<Integer> list = new ArrayList();
for (int i : array) {
if (i != value) {
list.add(i);
}
}
int[] newArray = new int[list.size()];
for (int i = 0; i < newArray.length; i++) {
newArray[i] = list.get(i);
}
return newArray;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class $ {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 4 };
show(arr);
arr = remove(arr, 4);
show(arr);
}
// 一次只能删除一个元素
private static int[] remove(int[] arr, int num) {
int[] tmp = new int[arr.length - 1];
int idx = 0;
boolean hasRemove = false;
for (int i = 0; i < arr.length; i++) {
if (!hasRemove && arr[i] == num) {
hasRemove = true;
continue;
}
tmp[idx++] = arr[i];
}
return tmp;
}
private static void show(int[] arr) {
for (int i : arr) {
System.out.print(i + ",");
}
System.out.println();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有直接删除的办法 ..
int[] a = {1,2,3,4,5};
//假设删除a[3]=4;
则
for(int i=3;i++;i<a.length-1){
a[i]=a[i+1]
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询