{
int [] num =new int[]{1,2,3,4,5,6,7,8,9,10}
Scanner input = new Scanner(System.in)
int temp=0
System.out.println("请输入你要删除的元素:")
int de=input.nextInt()
for(int i =0i<num.lengthi++)
{
if(num[i]==de)
{
for(int j = ij<num.length-1j++)
{
num[j]=num[j+1]
}
temp=1
break
}
}
if(temp==1)
{
System.out.println("删除此对象后数组值为:")
for(int i = 0i<num.length-1i++)
{
System.out.print(num[i]+"\t")
}
}
else
{
System.out.println("未找到你要删除的对象")
}
}
irb(main):001:0>a=[1,2,5,3,9,0,9]=>[1, 2, 5, 3, 9, 0, 9]
irb(main):002:0>a-[2]
=>[1, 5, 3, 9, 0, 9]
irb(main):004:0>a
=>[1, 2, 5, 3, 9, 0, 9]
irb(main):005:0>a-[4]
=>[1, 2, 5, 3, 9, 0, 9]
array_search(PHP 4 >= 4.0.5, PHP 5, PHP 7)
array_search — 在数组中搜索给定的值,如果成功则返回首个相应的键名
说明
array_search ( mixed $needle , array $haystack [, bool $strict = false ] ) : mixed
大海捞针,在大海(haystack)中搜索针( needle 参数)。
参数
needle
搜索的值。
注意:
如果 needle 是字符串,则比较以区分大小写的方式进行。
haystack
这个数组。
strict
如果可选的第三个参数 strict 为 true,则 array_search() 将在 haystack 中检查完全相同的元素。 这意味着同样严格比较 haystack 里 needle 的 类型,并且对象需是同一个实例。
返回值
如果找到了 needle 则返回它的键,否则返回 false。
如果 needle 在 haystack 中出现不止一次,则返回第一个匹配的键。要返回所有匹配值的键,应该用 array_keys() 加上可选参数 search_value 来代替。
警告
此函数可能返回布尔值 false,但也可能返回等同于 false 的非布尔值。请阅读 布尔类型章节以获取更多信息。应使用 === 运算符来测试此函数的返回值。
更新日志
版本 说明
5.3.0 As with all internal PHP functions as of 5.3.0, array_search() returns null if invalid parameters are passed to it.