oc self.array count values.count和_array count values.count为什么会有区别?

& 统计数组中所有的值出现的次数
PHP array_count_values 统计数组中所有的值出现的次数
array_count_values
array_count_values & 统计数组中所有的值出现的次数
array array_count_values
( array $input
Example #1 array_count_values() 例子
&?php$array&=&array(1,&"hello",&1,&"world",&"hello");print_r(array_count_values&($array));?&
以上例程会输出:
[hello] =& 2
[world] =& 1
PHP array_count_values note #1
i had a problem which was associated with the array_count_values function, but did not solve my problem realy well so i made a function by my self and maybe it can help also someone else.
The problem was that i had a table like this:
CL& && PRODNUM& & & & QN& & & DATE
1& & & & 2& & 10.12.2010
2& & & & 1& & 10.12.2010
3& & & & 1& & 10.12.2010
3& & & & 1& & 11.12.2010
3& & & & 2& & 11.12.2010
3& & & & 1& & 11.12.2010
1& & & & 1& & 10.12.2010
1& & & & 1& & 11.12.2010
(it go's on and on ... )
And i wanted to calculate for every unique date how much the quantity of the specific CL is.
For example the result should be like this:
10.12.2010: [1] -& 3
10.12.2010: [2] -& 1
10.12.2010: [3] -& 1
11.12.2010: [1] -& 1
11.12.2010: [2] -& 0
11.12.2010: [3] -& 4
So, i had to make a function which should look for unique values of a date and then for that date get the quantity of uniqe CL's.
For note: i put all the values of the table into an array:
$a = array();
$a['cl'] = 1;
$a['prodnum'] = '';
$a['qn'] = '2';
$a['date'] = '10.12.2010';
The function looks like this:
function csav( $array, $distinctV, $searchInV, $sumValue ){
&& & && $result = array();
&&& $array1 = array_unique($array[$searchInV]);
&&& $array2 = array();
&&& $unique1 = array_merge($array2, $array1);
&&& $array3 = array_unique($array[$distinctV]);
&&& $array4 = array();
&&& $unique2 = array_merge($array4, $array3);
&&& for($k = 0; $k & count($unique1); $k++){
&& & && for($i = 0; $i & count($unique2); $i++){
&& & & & && $sum = 0;
&& & & & && for($j = 0; $j & count($array[$distinctV]); $j++){
&& & & & &&
&& & & & & & && if($array[$distinctV][$j] == $unique2[$i] and $array[$searchInV][$j] == $unique1[$k])
&& & & & & & & & && $sum += $array[$sumValue][$j];
&& & & & && }
&& & & & && $result[$unique1[$k]][$unique2[$i]] = $sum;
&&& return $result;
The use of function has to be like this:
$result = csav($a, 'cl', 'date', 'qn');
print_r($result);
And the result looks like this:
Array ( [] =& Array ( [1] =& 14 [2] =& 17 [3] =& 4 ) [] =& Array ( [1] =& 13 [2] =& 17 [3] =& 4 ) [] =& Array ( [1] =& 13 [2] =& 17 [3] =& 4 ) [] =& Array ( [1] =& 14 [2] =& 17 [3] =& 4 ) [] =& Array ( [1] =& 14 [2] =& 17 [3] =& 2 ) )
This is it.
Hope it helps someone.
PHP array_count_values note #2
Here is a Version with one or more arrays, which have similar values in it:
Use $lower=true/false to ignore/set case Sensitiv.
$ar1[] = array("red","green","yellow","blue");
$ar1[] = array("green","yellow","brown","red","white","yellow");
$ar1[] = array("red","green","brown","blue","black","yellow");
$res = array_icount_values ($ar1);
print_r($res);
function array_icount_values($arr,$lower=true) {
&& & $arr2=array();
&& & if(!is_array($arr['0'])){$arr=array($arr);}
&& & foreach($arr as $k=& $v){
&& && foreach($v as $v2){
&& && if($lower==true) {$v2=strtolower($v2);}
&& && if(!isset($arr2[$v2])){
&& & & && $arr2[$v2]=1;
&& && }else{
&& & & & & $arr2[$v2]++;
&& & & & & }
&&& return $arr2;
PHP array_count_values note #3
byron at byronrode dot co dot za, here are some benchmarks.
$haystack = Array();
for ($i = 0; $i & 1000000; $i++) {
& $haystack[] = rand(1, 2000);
$needle = rand(1, 2000);
echo "__array_count_values()__
$start = microtime(true);
$startmem = memory_get_usage();
$counts = array_count_values($haystack);
$mem = memory_get_usage()-$startmem;
echo 'Count:'.$counts[$needle]."
echo 'Time:'.(microtime(true) - $start)."
echo 'Memory:'.$mem."
echo "__array_keys()__
$start = microtime(true);
$startmem = memory_get_usage();
$keys = array_keys($haystack, $needle);
$mem = memory_get_usage()-$startmem;
echo 'Count:'.count($keys)."
echo 'Time:'.(microtime(true) - $start)."
echo 'Memory:'.$mem."
echo '__$needle_array[]__'."
$start = microtime(true);
$startmem = memory_get_usage();
$x = count($haystack);
for($i = 0; $i & $x; $i++){
& if($haystack[$i] == $needle){
&&& $needle_array[] = $haystack[$i];
$mem = memory_get_usage()-$startmem;
$number_of_instances = count($needle_array);
echo 'Count:'.$number_of_instances."
echo 'Time:'.(microtime(true) - $start)."
echo 'Memory:'.$mem."
echo '__$number_of_instances++__'."
$start = microtime(true);
$startmem = memory_get_usage();
$x = count($haystack);
$number_of_instances = 0;
for($i = 0; $i & $x; $i++){
& if($haystack[$i] == $needle){
&&& $number_of_instances++;
$mem = memory_get_usage()-$startmem;
echo 'Count:'.$number_of_instances."
echo 'Time:'.(microtime(true) - $start)."
echo 'Memory:'.$mem."
[www]mytemp$ php array_count_test.php
__array_count_values()__
Memory:120328
__array_keys()__
Memory:33016
__$needle_array[]__
Memory:24792
__$number_of_instances++__
However, when you use an array of strings by calling md5(rand(1, 2000)), the performance boosts become less significant:
__array_count_values()__
Memory:184328
__array_keys()__
Memory:30072
__$needle_array[]__
Memory:22104
__$number_of_instances++__
Results are similar for string-&string haystacks with foreach traversal.
PHP array_count_values note #4
Yet Another case-insensitive version of array_count_values()
$ar = array('J. Karjalainen', 'J. Karjalainen', 60, '60', 'J. Karjalainen', 'j. karjalainen', 'Fastway', 'FASTWAY', 'Fastway', 'fastway', 'YUP');
$ar = array_count_values(array_map('strtolower', $ar));
PHP array_count_values note #5
I am building a script for a quiz, and could not find any answers to count the number of times a value was repeated in an array, and came up with the following function.
$array = array('a', 'b', 'a', 'a', 'c', 'a', 'd', 'a', 'c', 'd');
function count_repeat_values($needle, $haystack){
&&& $x = count($haystack);
&&& for($i = 0; $i & $x; $i++){
&& & && if($haystack[$i] == $needle){
&& & & & && $needle_array[] = $haystack[$i];
&&& $number_of_instances = count($needle_array);
&&& return $number_of_instances;
echo count_repeat_values('a', $array);
But after writing the function, I happened to stroll upon array_count_values() which I had completely forgotten about.
I know that i could get the value by doing this:
$array = array('a', 'b', 'a', 'a', 'c', 'a', 'd', 'a', 'c', 'd');
$answer = array_count_values($array);
echo $answer['a']
Would be interesting to see which version works quicker...
PHP array_count_values note #6
function array_icount_values($array) {
&&& $ret_array = array();
&&& foreach($array as $value) $ret_array[strtolower($value)]++;
&&& return $ret_
$ar = array('J. Karjalainen', 'J. Karjalainen', 60, '60', 'J. Karjalainen', 'j. karjalainen', 'Fastway', 'FASTWAY', 'Fastway', 'fastway', 'YUP');
$ar = array_icount_values($ar);
this prints:
&&& [j. karjalainen] =& 4
&&& [60] =& 2
&&& [fastway] =& 4
&&& [yup] =& 1
PHP array_count_values note #7
I couldn't find a function for counting the values with case-insensitive matching, so I wrote a quick and dirty solution myself:
&pre&&?php
function array_icount_values($array) {
&&& $ret_array = array();
&&& foreach($array as $value) {
&& & && foreach($ret_array as $key2 =& $value2) {
&& & & & && if(strtolower($key2) == strtolower($value)) {
&& & & & & & && $ret_array[$key2]++;
&& & & & & & && continue 2;
&& & & & && }
&& & && $ret_array[$value] = 1;
&&& return $ret_array;
$ar = array('J. Karjalainen', 'J. Karjalainen', 60, '60', 'J. Karjalainen', 'j. karjalainen', 'Fastway', 'FASTWAY', 'Fastway', 'fastway', 'YUP');
$ar2 = array_count_values($ar); $ar = array_icount_values($ar); print_r($ar2);
print_r($ar);
This prints:
&&& [J. Karjalainen] =& 3
&&& [60] =& 2
&&& [j. karjalainen] =& 1
&&& [Fastway] =& 2
&&& [FASTWAY] =& 1
&&& [fastway] =& 1
&&& [YUP] =& 1
&&& [J. Karjalainen] =& 4
&&& [60] =& 2
&&& [Fastway] =& 4
&&& [YUP] =& 1
I don't know how efficient it is, but it seems to work. Needed this function in one of my scripts and thought I would share it.
PHP array_count_values note #8
Scratch that, I did something stupid. Here is a better function.
&&& function array_enumerate_keys($array)
&& & && $index = 0;
&& & && $enumerated = array();
&& & && $values = array_values($array);
&& & && $keys = array_keys($array);
&& & && for($index = 0; $index & count($array); $index++)
&& & & & && $iteration;
&& & & & && for($iteration = 0; $iteration & $values[$index]; $iteration++)
&& & & & && {
&& & & & & & && $enumerated = array_merge($enumerated, array($keys[$index]));
&& & & & && }
&& & && return $enumerated;
PHP array_count_values note #9
A possible inverse function for array_count_values
&&& function array_enumerate_keys($array)
&& & && $index;
&& & && $enumerated;
&& & && $values = array_values($array);
&& & && $keys = array_keys($array);
&& & && for($index = 0; $index & count($array); $index++)
&& & & & && $iteration;
&& & & & && for($iteration = 0; $iteration & $values[$index]; $iteration++)
&& & & & && {
&& & & & & & && $enumerated .= $keys[$index] . ',';
&& & & & && }
&& & && return explode(',', $enumerated);
PHP array_count_values note #10
alwaysdrunk's comment only works if you can trust the client web browser. Using this function doesn't validate that every necessary field exists -- only that every field that was submitted has a value in it. Thus if an attacker wished to force a null value into one of the fields, he could (rather easily) construct a modified form without the field and submit THAT.
Besides, you really ought to be validating each field anyway if you're taking user input.
PHP array_count_values note #11
if you have too many values in $_POST,$_GET array that needs to be controlled with isset() in oreder to understand the form is filled completely and have no empty text boxes.
you can try this,it saves time.
$n = array_count_values($_POST);
if (!isset($n[''])) {
echo "The form is filled completely";
{ die("Please fill the form comlpetely"); }
//tested in php 5
PHP array_count_values note #12
I find a very simple solution to count values in multidimentional arrays (example for 2 levels) :
foreach ($array as $a) {
& foreach ($a as $b) {
&&& $count_values[$b]++;
PHP array_count_values note #13
my solution for count& on multidimentional arrays.
& for($i = 0; $i & count($array); $i++) {
&& && $detail = explode("|", $array[$i]);
&& && echo "$i - $detail[0] - $detail[1]&br&&br&";
&& && if($detail[1] == '1') { $wieoft1 = $wieoft1 +=1; }
&& && if($detail[1] == '2') { $wieoft2 = $wieoft2 +=1; }
&& && if($detail[1] == '3') { $wieoft3 = $wieoft3 +=1; }
& echo ". $wieoft1 : $wieoft2 : $wieoft3";
looks not pretty fine yet works great for me.
make it bigger for your own.
PHP array_count_values note #14
array_count_values returns the number of keys if empty(value). I expected array_count_values to return 0 for empty values.
Array looks like:
&& & & & && [459] =&
&& & & & && [543] =&
&& & & & && [8959] =&
&& & & & && [11273] =&
array_count_values returns:
&&& [] =& 4
count(array_count_values(array)) does thus not report there are no values (other than empty) in the array.
I therefore check:
$arrFoo=array_count_values($arrBar);
if(isset($arrFoo[""]) $allempty=count($arrBar)==$arrFoo[""];
if(!$allempty)
//process the array
//no need to work on the array
PHP array_count_values note #15
I fount a solution for the count of array elements in the sense of array_count_values, but i was not able to use the function array_count_values itself because it does not say me if arrays exists in the given array, so i had to use a foreach loop and a little)
function array_count_values_multidim($a,$out=false) {
& if ($out===false) $out=array();
& if (is_array($a)) {
&&& foreach($a as $e)
&& && $out=array_count_values_multidim($e,$out);
&&& if (array_key_exists($a,$out))
&& && $out[$a]++;
&& && $out[$a]=1;
& return $out;
PHP array_count_values note #16
array_count_values function does not work on multidimentional arrays.
If $score[][] is a bidimentional array, the command
"array_count_values ($score)" return the error message "Warning: Can only count STRING and INTEGER values!".
PHP array_count_values note #17
You might use serialize() to serialize your objects before analyzing their frequency. :)
PHP array_count_values note #18
suggested plan of attack:
class MyObject {
&&& function MyObject($t = 'none')
&& && $this-&$myTag = $t;
$myArray = array();
for ($i = 1 ; $i & 11 ; $i++)
&& $myobj = new MyObject( str_pad('n', $i, 'x') );
&& $myArray[ $myobj-&$myTag ] = $
print_r( array_count_values(array_keys($myArray)) );
to sum up:
assuming each instance of an object you create has some sort of tag, e.g.,
$this-&$myTag=get_class($this)
..you should be set. objects dont have value to compare the way strings and integers do, so, $myTag's value is arbitrary.
PHP array_count_values note #19
This does not works with objects. If you have an array filled with objects, you can not count them.
$myArray = array();
for ($i = 0 ; $i & 10 ; $i++)
&& $myObject = new MyObject();
&& $myArray[$i] = $myObject;
echo (array_count_values($myArray));
This gives you:
Warning: Can only count STRING and INTEGER values...
Found no solution for this yet...
PHP数组 - 函数13158人阅读
在之前的一篇文章中介绍了Foundation框架中的NSString类和NSMutableString类:今天我们继续来看一下Foundation框架中的NSArray类和NSMutableArray类,其实NSArray类和Java中的List差不多,算是一种数据结构,当然我们从这两个类可以看到,NSArray类是不可变的,NSMutableArray类是可变的。下面就先来看一下NSArray类一、NSArray类//
16_NSArray
Created by jiangwei on 14-10-12.
Copyright (c) 2014年 jiangwei. All rights reserved.
#import &Foundation/Foundation.h&
//NSArray中不可以存放基本数据类型,只能存放类的实例,如果需要将基本类型、结构体放入到数组中得话,
//需要通过NSNumber/NSValue进行数据的封装,同时不能在NSArray中存储nil
int main(int argc, const char * argv[]) {
@autoreleasepool {
//1.---------------------数组的创建
NSString *s1 = @&zhangsan&;
NSString *s2 = @&lisi&;
NSString *s3 = @&wangwu&;
//最后的nil相当于结束的标记
NSArray *array1 = [[NSArray alloc] initWithObjects:s1,s2,s3, nil];
//打印的时候会调用description方法
//相当于:array1.description
NSLog(@&%@&,array1);
//使用类方法创建
array1 = [NSArray arrayWithObjects:s1,s2,s3, nil];
//创建一个数组,将源数组中的数据拿出来放进去
NSArray *array2 = [NSArray arrayWithArray:array1];
//2.----------------------objectAtIndex
//访问数组中的数据,数组中存放的是对象的指针
NSString *str1 = [array1 objectAtIndex:0];
//3.----------------------count
//获取数组元素中的个数
NSUInteger count = [array1 count];//返回的是一个无符号数值
//4.----------------------containsObject
//判断数组中是否包含某一个对象,判断的是指针对象值,不是对象中的值
BOOL isContains = [array1 containsObject:@&zhangsan&];
//5.----------------------indexOfObject
//返回一个对象在数组中的下标值
NSUInteger index = [array1 indexOfObject:@&zhangsan&];
if(index == NSNotFound){
//没有找到
//6.----------------------componentsJoinedByString
//数组中存放的如果是字符串的话,可以使用连接符将其所有的元素进行连接
//注意数组中的元素必须都是字符串
NSString *content = [array1 componentsJoinedByString:@&,&];
//7.----------------------lastObject
//访问数组最后一个元素
NSString *lastObj = [array1 lastObject];
//8.----------------------arrayByAddingObject
//在原来的数组后面追加元素,返回一个新的数组对象,因为他是不可变的
NSArray *array3 = [array1 arrayByAddingObject:@&zhaoliu&];
//数组的遍历
for(int i=0;i&array1.i++){
NSString *str = [array1 objectAtIndex:i];
NSLog(@&%@&,str);
//快速遍历
for(NSString *s in array1){
NSLog(@&%@&,s);
//xcode4.4以后,编译器对数组的创建和访问语法做了优化
NSArray *array7 = @[s1,s2,s3];
NSString *s = array7[0];
下面来看一下他的操作方法:1、NSArray的创建//1.---------------------数组的创建
NSString *s1 = @&zhangsan&;
NSString *s2 = @&lisi&;
NSString *s3 = @&wangwu&;
//最后的nil相当于结束的标记
NSArray *array1 = [[NSArray alloc] initWithObjects:s1,s2,s3, nil];
//打印的时候会调用description方法
//相当于:array1.description
NSLog(@&%@&,array1);我们看到他的initWithObjects方法的最后一个值为nil,这个我们在之前说到过这个值,他是空指针的意思,和Java中的null一样的,这里为什么在创建NSArray的时候最后一个值为空呢?这个和C语言中的场景是类似的,C语言中字符串的结束符为'/0',那么这里的NSArray的最后一个元素为nil,原因是标记NSArray结束了。然后我们调用NSLog方法打印NSArray对象,结果:我们看到了,会打印出非常格式化的数据出来,这个原因是因为NSLog打印对象的时候会调用其description方法,和Java中的toString方法一样,当然我们可以重写这个方法的,后面会说到。我们还可以使用类方法创建NSArray://使用类方法创建
array1 = [NSArray arrayWithObjects:s1,s2,s3, nil];
//创建一个数组,将源数组中的数据拿出来放进去
NSArray *array2 = [NSArray arrayWithArray:array1];2、使用下标访问元素//2.----------------------objectAtIndex
//访问数组中的数据,数组中存放的是对象的指针
NSString *str1 = [array1 objectAtIndex:0];3、获取数组的大小//3.----------------------count
//获取数组元素中的个数
NSUInteger count = [array1 count];//返回的是一个无符号数值NSUInteger是无符号的int类型4、是否包含某一个元素//4.----------------------containsObject
//判断数组中是否包含某一个对象,判断的是指针对象值,不是对象中的值
BOOL isContains = [array1 containsObject:@&zhangsan&];5、找到一个元素在数组中的下标//5.----------------------indexOfObject
//返回一个对象在数组中的下标值
NSUInteger index = [array1 indexOfObject:@&zhangsan&];
if(index == NSNotFound){
//没有找到
}这里我们看到用到了一个系统常量:NSNotFound,我们可以看到他的定义:#define NSIntegerMax
LONG_MAX看到了,是一个Long类型的最大值。6、使用指定的连接符连接数组中所有的元素//6.----------------------componentsJoinedByString
//数组中存放的如果是字符串的话,可以使用连接符将其所有的元素进行连接
//注意数组中的元素必须都是字符串
NSString *content = [array1 componentsJoinedByString:@&,&];7、在数组的尾部增加一个元素//8.----------------------arrayByAddingObject
//在原来的数组后面追加元素,返回一个新的数组对象,因为他是不可变的
NSArray *array3 = [array1 arrayByAddingObject:@&zhaoliu&];因为NSArray是不可变的,所以会产生一个新的NSArray对象,返回回来8、数组的遍历//数组的遍历
for(int i=0;i&array1.i++){
NSString *str = [array1 objectAtIndex:i];
NSLog(@&%@&,str);
//快速遍历
for(NSString *s in array1){
NSLog(@&%@&,s);
}第二种方式和Java中的快速遍历是一样的。9、Xcode4.4之后新增了NSArray快速创建的一种方式//xcode4.4以后,编译器对数组的创建和访问语法做了优化
NSArray *array7 = @[s1,s2,s3];
NSString *s = array7[0];这种方式比之前的创建方式又快又方便,而且符合正常的创建方式,访问的时候也是可以直接用下标进行获取元素二、NSMutableArray类NSArray类是不可变的,NSMutableArray类是可变的,可变类一样的特性,就是可以进行增删改查操作//
17_NSMutableArray
Created by jiangwei on 14-10-12.
Copyright (c) 2014年 jiangwei. All rights reserved.
#import &Foundation/Foundation.h&
//NSMutableArray继承NSArray类,有NSArray中的所有方法
int main(int argc, const char * argv[]) {
@autoreleasepool {
//1.---------------------创建可变数组
NSString *str1 = @&zhangsan&;
NSString *str2 = @&lisi&;
NSString *str3 = @&wangwu&;
NSMutableArray *mArray1 = [[NSMutableArray alloc] initWithObjects:str1,str2,str3, nil];
//下面的这种方式创建的数组是错误的
//下面的这种方式是创建不可变数组的方式,不能哟关于可变数组的创建
//NSMutableArray *array1 = @[str1,str2,str3];
//先开辟5个空间,用于存储元素,当存储的元素超过了5个,数组会自动增大空间
NSMutableArray *mArray2 = [[NSMutableArray alloc] initWithCapacity:5];
//使用类方法创建
NSMutableArray *mArray3 = [NSMutableArray arrayWithCapacity:5];
//2.---------------------addObject
//添加元素的方法
[mArray1 addObject:str1];
[mArray1 addObject:str2];
//添加数组,把mArray1中的所有元素全部添加到mArray2中
[mArray2 addObjectsFromArray:mArray1];
NSLog(@&mArray3 = %@&,mArray2);
//二维数组
//这个是将mArray1添加到mArray2数组中,这样mArray2就变成了二维数组了
[mArray2 addObject:mArray1];
//3.---------------------insertObject
//在指定的位置上插入特定元素
[mArray2 insertObject:@&def& atIndex:0];
//4.---------------------replaceObjectAdIdex
//替换元素
[mArray2 replaceObjectAtIndex:0 withObject:@&aaa&];
//5.---------------------exchangeObjectAtIndex
//互换两个元素的位置
[mArray2 exchangeObjectAtIndex:0 withObjectAtIndex:3];
//6.---------------------removeObjectAdIndex
//删除指定位置的元素
[mArray2 removeObjectAtIndex:0];
//删除最后一个元素
[mArray2 removeLastObject];
//删除指定的对象
[mArray2 removeObject:@&lisi&];
//删除所有的对象/清空列表
[mArray2 removeAllObjects];
1、创建方法//1.---------------------创建可变数组
NSString *str1 = @&zhangsan&;
NSString *str2 = @&lisi&;
NSString *str3 = @&wangwu&;
NSMutableArray *mArray1 = [[NSMutableArray alloc] initWithObjects:str1,str2,str3, nil];
//下面的这种方式创建的数组是错误的
//下面的这种方式是创建不可变数组的方式,不能用于可变数组的创建
//NSMutableArray *array1 = @[str1,str2,str3];
//先开辟5个空间,用于存储元素,当存储的元素超过了5个,数组会自动增大空间
NSMutableArray *mArray2 = [[NSMutableArray alloc] initWithCapacity:5];
//使用类方法创建
NSMutableArray *mArray3 = [NSMutableArray arrayWithCapacity:5];创建方式和NSArray类似,但是有一种方式不能用,就是直接创建的方式,那个只能用于创建不变数组。但是可变数组还有一个方法就是能够事先的设置数组的大小,而且超过这个大小之后,数组会自动扩充,类似于C语言中的动态数组的实现。2、添加元素//2.---------------------addObject
//添加元素的方法
[mArray1 addObject:str1];
[mArray1 addObject:str2];
//添加数组,把mArray1中的所有元素全部添加到mArray2中
[mArray2 addObjectsFromArray:mArray1];
NSLog(@&mArray3 = %@&,mArray2);
//二维数组
//这个是将mArray1添加到mArray2数组中,这样mArray2就变成了二维数组了
[mArray2 addObject:mArray1];
NSLog(@&mArray3 = %@&,mArray2);使用addObjectsFromArray方法是将一个数组中的每个元素添加到指定的数组中使用addObject方法是讲一个数组整个都添加到指定的数组中了,那么这个数组就变成二维数组了这个两个方法要区分一下~~下面是运行结果:3、在数组的指定位置上插入元素//3.---------------------insertObject
//在指定的位置上插入特定元素
[mArray2 insertObject:@&def& atIndex:0];4、替换元素//4.---------------------replaceObjectAdIdex
//替换元素
[mArray2 replaceObjectAtIndex:0 withObject:@&aaa&];5、互换两个元素的位置//5.---------------------exchangeObjectAtIndex
//互换两个元素的位置
[mArray2 exchangeObjectAtIndex:0 withObjectAtIndex:3];6、删除方法//6.---------------------removeObjectAdIndex
//删除指定位置的元素
[mArray2 removeObjectAtIndex:0];
//删除最后一个元素
[mArray2 removeLastObject];
//删除指定的对象
[mArray2 removeObject:@&lisi&];
//删除所有的对象/清空列表
[mArray2 removeAllObjects];总结这篇文章就介绍了Foundation框架中的NSArray类和NSMutableArray类,他们是用来存放一些指定类型的元素的注:OC中没有泛型的概念,所以对于数组中存放了不同类型的值,在运行的时候会报错的,这个和Java相比,操作集合类的时候安全性就差了点。所以我们在操作集合类的时候需要注意的地方。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
扫一扫加小编微信
添加时请注明:编码美丽,否则不予通过!
我的第一个App: 天真无谐
已经上线了,在各大市场都可以搜索到。希望大家多多支持!!
访问:3246417次
积分:25147
积分:25147
排名:第198名
原创:235篇
评论:17026条
文章:29篇
阅读:321837
文章:23篇
阅读:212472
文章:25篇
阅读:351517
文章:30篇
阅读:272464
文章:64篇
阅读:879747
(1)(5)(4)(8)(7)(8)(5)(5)(8)(1)(4)(4)(2)(1)(4)(3)(2)(4)(1)(1)(1)(30)(9)(4)(7)(12)(3)(12)(13)(6)(20)(6)(32)(32)

我要回帖

更多关于 array count value 的文章

 

随机推荐