%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
读取csv文件中读取:一次读入全部(设置headers使 CSV#shift() 以CSV::Row对象返回而不是数组;使
require'csv'CSV#read() 返回 CSV::Table 对象而不是数组)CSV.read('test.csv')#=>Array#headers默认为false,如果设置为true,csv的第一行将被视为标题CSV.read('test.csv',headers:true)#=>CSV::Table#headers设置为数组,这个数组将被作为标题CSV.read('test.csv',headers:[1,2,3,4,5])#headers设置为字符串,这个字符串内容将被作为标题CSV.read('test.csv',headers:"1,2,3,4,5")
文件中读取:一次读入一行
#由于headers配置,返回类型发生变化(这个方法默认为第一行是标题,不会进行返回)CSV.foreach'test.csv'do|row|puts row.class#=>ArrayendCSV.foreach('test.csv',headers:true)do|row|puts row.class#=>CSV::Rowend#return_headers:true 返回标题CSV.foreach('test.csv',return_headers:true)do|row|p row#=>返回Arrayend
字符串中读取:一次读取一行
CSV.parse("CSV,data,String")do|row|# use row here...end
字符串中读取:全部读取
CSV.parse("CSV,data,String")#[]方法需要返回的类型为CSV::ROW所以设置参数headers:truecontent = File.read('data.csv') csv = CSV.parse(content,headers:true) sum =0csv.eachdo|row|sum += row['id'].to_iendputs sum
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("未找到你要删除的对象")
}
}