Anybody got any idea on how I can check if an array indexes(not just one index) are empty and if is empty or zero put a value there. And also if all the indexes are all not empty print an error.
8 Replies - 2340 Views - Last Post: 21 March 2012 - 06:35 PM
#1
How to check if array indexes are empty and if so check the next?
Posted 20 March 2012 - 03:51 AM
Replies To: How to check if array indexes are empty and if so check the next?
#2
Re: How to check if array indexes are empty and if so check the next?
Posted 20 March 2012 - 03:59 AM
If it is an array of Object the entry will be null
If it is an array of long, int or double or float the entry will be 0L, 0, 0.0, 0.0f
If it is an array of boolean the entry will be false
So for a basic datatype no really way to check if it is empty or if the value is not effectively 0
If it is an array of long, int or double or float the entry will be 0L, 0, 0.0, 0.0f
If it is an array of boolean the entry will be false
So for a basic datatype no really way to check if it is empty or if the value is not effectively 0
#3
Re: How to check if array indexes are empty and if so check the next?
Posted 20 March 2012 - 04:01 AM
Yes.
I assume you mean to check whether the array elements are empty rather than the indices. If so, use a loop to iterate the array, check for empty elements using an if statement - however you define empty - and if the array element is empty, set it to the desired value.
Your last question is a bit confusing, but I think you you want to flag an error if all array elements have been filled (are non-empty). This can be included in the "if empty" branch: set a flag before beginning the array iteration, boolean error = true; and if the "if empty" branch is entered, set the error = false;
I assume you mean to check whether the array elements are empty rather than the indices. If so, use a loop to iterate the array, check for empty elements using an if statement - however you define empty - and if the array element is empty, set it to the desired value.
Your last question is a bit confusing, but I think you you want to flag an error if all array elements have been filled (are non-empty). This can be included in the "if empty" branch: set a flag before beginning the array iteration, boolean error = true; and if the "if empty" branch is entered, set the error = false;
#4
Re: How to check if array indexes are empty and if so check the next?
Posted 20 March 2012 - 04:03 AM
pbl, on 20 March 2012 - 03:59 AM, said:
If it is an array of Object the entry will be null
If it is an array of long, int or double or float the entry will be 0L, 0, 0.0, 0.0f
If it is an array of boolean the entry will be false
So for a basic datatype no really way to check if it is empty or if the value is not effectively 0
If it is an array of long, int or double or float the entry will be 0L, 0, 0.0, 0.0f
If it is an array of boolean the entry will be false
So for a basic datatype no really way to check if it is empty or if the value is not effectively 0
Thanks. Is an integer array.
- But how do check for more than one index?
- If the index is empty how do check for the next index and if that one is not empty how do i check the next one, etc?
#5
Re: How to check if array indexes are empty and if so check the next?
Posted 20 March 2012 - 05:16 AM
When you create an array, all slots are created and intialized to 0 fior an array of int
so
should print:
Nb elements at 0: 98
as only slot 5 and 20 have a value != 0
Happy coding
As we are talking about an int array
They just can't be empty. They can contain 0 but not being empty
so
int[] x = new int[100];
x[5] = 123;
x[20] = 456;
int count = 0;
for(int i = 0; i < x.length; ++i) {
if(x[i] == 0)
++count;
}
System.out.println("Nb elements at 0: " + count);
should print:
Nb elements at 0: 98
as only slot 5 and 20 have a value != 0
Happy coding
As we are talking about an int array
Tsukuyomi, on 20 March 2012 - 06:51 AM, said:
are empty and if is empty or zero put a value there.
They just can't be empty. They can contain 0 but not being empty
This post has been edited by pbl: 20 March 2012 - 05:13 AM
#6
Re: How to check if array indexes are empty and if so check the next?
Posted 20 March 2012 - 05:22 AM
pbl, on 20 March 2012 - 05:16 AM, said:
When you create an array, all slots are created and intialized to 0 fior an array of int
so
should print:
Nb elements at 0: 98
as only slot 5 and 20 have a value != 0
Happy coding
As we are talking about an int array
They just can't be empty. They can contain 0 but not being empty
so
int[] x = new int[100];
x[5] = 123;
x[20] = 456;
int count = 0;
for(int i = 0; i < x.length; ++i) {
if(x[i] == 0)
++count;
}
System.out.println("Nb elements at 0: " + count);
should print:
Nb elements at 0: 98
as only slot 5 and 20 have a value != 0
Happy coding
As we are talking about an int array
Tsukuyomi, on 20 March 2012 - 06:51 AM, said:
are empty and if is empty or zero put a value there.
They just can't be empty. They can contain 0 but not being empty
import java.util.Scanner;
public class Myhash {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] myData = new int[17];
int [] newData = new int [17];
System.out.println("Please enter 16 integer numbers");
for(int i= 0; i<myData.length; i++){
//System.out.print("Please enter 16 numbers");
Scanner input = new Scanner(System.in);
int data =input.nextInt();
myData[i]=data;
int num3 = data % 17;
newData[num3]=data;
}
System.out.println("These are the numbers you entered\n");
System.out.printf("%s%8s \n", "Index", "Value");
for(int t=0; t<myData.length; t++){
System.out.printf("%5d%8d\n", t, myData[t]);
}
System.out.println("\n");
System.out.println("The Hash Function:\n\n");
System.out.printf("%5s%8s \n", "Index", "Value");
for(int s=0; s<newData.length; s++){
System.out.printf("%5d%8d\n", s, newData[s]);
}
}
}
on here:
for(int s=0; s<newData.length; s++){
System.out.printf("%5d%8d\n", s, newData[s]);
}
#7
Re: How to check if array indexes are empty and if so check the next?
Posted 20 March 2012 - 01:41 PM
#8
#9
Re: How to check if array indexes are empty and if so check the next?
Posted 21 March 2012 - 06:35 PM
Don't want to be rude but there ia something about array concept you really don't understand
for(int s=0; s<newData.length; s++){
at the first iteration of the loop the array index "s" will be 0
then you will scan all the other array index (and they are all different than 0
)
TriggaMike's Arrays tutorial
xor-logic's Arrays tutorial
for(int s=0; s<newData.length; s++){
at the first iteration of the loop the array index "s" will be 0
then you will scan all the other array index (and they are all different than 0
TriggaMike's Arrays tutorial
xor-logic's Arrays tutorial
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|