![](/img/trans.png)
[英]How to find the number of times a specific element occurs in an Array list in Java
[英]How to find number of element in an Array in java?
我想知道如何在java中找到普通数组中的元素数。 例如,如果我有一个大小为 10 的 int 数组,并且我只插入了 5 个元素。 现在,我想检查数组中的元素数量? 以下是更多说明的代码。 请帮忙
public static void main(String[] args) {
int [] intArray = new int[10];
char [] charArray = new char[10];
int count = 0;
for(int i=0; i<=5; i++) {
intArray[i] = 5;
charArray[i] = 'a';
}
for(int j=0; j<=intArray.length; j++) {
if(intArray[j] != null) {
count++;
}
}
System.out.println("int Array element : "+ count);
}
代码错了。 你声明一个int[]
:它不能包含空值,所以这个语句不会编译:
if(intArray[j] != null) {
然后,如果你想存储很多项目但你不知道有多少,你应该使用Collection
,例如ArrayList
List<Integer> ints = new ArrayList<Integer>(); //Or new ArrayList<>(); - Java7
然后,您可以使用以下尺寸:
ints.size();
现在,如果你真的想使用一个数组,那么你可以例如计算非零值的数量:
for(int j=0; j<=intArray.length; j++) {
if(intArray[j] != 0) {
count++;
}
}
或者在Java 7 中更好:
for(int i : intArray) {
if(i!=0) {
count++;
}
}
在Java 8 中甚至更好:
Arrays.stream(intArray).filter(i -> i !=0).count();
查看Collection.frequency()
。 这将计算特定对象(在本例中为null
)在集合中出现的次数。
下面只是一个例子来帮助你
String[] array = new String[5];
array[0] = "This";
array[1] = null;
array[2] = "is";
array[3] = null;
array[4] = "a test";
int count = Collections.frequency(Arrays.asList(array), null);
System.out.println("String: " + count + " items out of " + array.length + " are null");
int[] iArray = new int[3];
iArray[0] = 0;
iArray[1] = 1;
iArray[2] = 2;
List<Integer> iList = new ArrayList<>();
for (int i : iArray) {
iList.add(i);
}
int iCount = Collections.frequency(iList, 0);
System.out.println("Int: " + iCount + " items out of " + iArray.length + " are zero");
char[] cArray = new char[3];
cArray[0] = 'c';
cArray[1] = ' ';
cArray[2] = 'a';
List<Character> cList = new ArrayList<>();
for (char c : cArray) {
cList.add(c);
}
int cCount = Collections.frequency(cList, ' ');
System.out.println("Char: " + cCount + " items out of " + cArray.length + " are ' '");
输出:
字符串:5 项中有 2 项为空
Int:3 项中有 1 项为零
字符:3 项中有 1 项是 ' '
您可以将第一个循环更改为如下所示:
for(int i=0; i<=5; i++) {
intArray[i] = 5;
charArray[i] = 'a';
count++;
}
或者更好的是,不要使用数组,而是使用 ArrayList。 下面是一个数组列表如何工作的例子。
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Character> charList = new ArrayList<Character>();
for (int i = 0; i < 5; i++){
intList.add(5);
charList.add('a');
}
System.out.println("int list element : " + intList.size());
ArrayList 是动态的并且.size()
将只返回当前在 arraylist 中的项目的大小。
如果您想实现自己的解决方案来计算数组中的元素,您可以随时使用其中的array
实现自己的包装类。
public class classWithArray {
private int[] arr;
private int count;
public classWithArray(int arrLength) {
arr = new int[5];
count = 0;
}
public void add(int num) {
arr[count] = num;
count++;
}
public int getCount() {
return count;
}
public int getElement(int index) {
return arr[index];
}
// Add other wrapper methods.
}
您可以创建Generic
类型的array
(尽管我对Java
了解不多)以使其适用于任何数据类型。
好吧,我们可以对数组中的非零元素使用计数,代码如下:
public class SomeElementOfArrayUsingCount{
public static void main(String[] args){
double total;//The sum of non-zero numbers in the Array.
total=0;
double average;//The average of non-zero numbers.
int i;
double count;//The number of non-zero numbers.
count = 0;
int[] list;
list = new int[5];//make a container of 5.
list[0]=2;
list[1]=4;
list[2]=4;
list[3]=5;
list[4]=2;
for(i=0;i<list.length;i++){
if(list[i]!=0){
total = total + list[i];//Add element to the array
count = count + 1;//and Count it
}
}
if(count==0){
System.out.println("There were no non-zero elements");
}
else {
average = total/count;//Divide by number of items
System.out.println("The average is " + average + " the total count is " + count);
}
}
}
系统输出(); 这将有助于您的流程,因为它将打印出您正在寻找的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.