%w(a b c a c d).uniq
得到各个元素出现的个数:
count_hash = {}
%w(a b c a c d).each do |item|
key = item.to_sym
if count = count_hash[key]
count_hash[key] = count + 1
else
count_hash[key] = 1
end
end
终于明白你的意思了。这不是 hash 的问题,是 Array.new 的用法不对,你这样做是指用{}这个对象填充3遍,就是说是同一个对象填充了3次,所以无论你修改哪一个对象,其它的都会跟着变,因为是同一对象。
http://www.ruby-doc.org/core-2.2.0/Array.html#method-c-new-label-Common+gotchas
以上是 ruby 文档,他给出了正确的用法:
a = Array.new(2) { Hash.new }public static void main(String[] args){
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("未找到你要删除的对象")
}
}