8 Replies - 15187 Views - Last Post: 21 March 2012 - 06:35 PM Rate Topic: -----

#1 Tsukuyomi  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 98
  • Joined: 22-February 11

How to check if array indexes are empty and if so check the next?

Posted 20 March 2012 - 03:51 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: How to check if array indexes are empty and if so check the next?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

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;
Was This Post Helpful? 0
  • +
  • -

#4 Tsukuyomi  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 98
  • Joined: 22-February 11

Re: How to check if array indexes are empty and if so check the next?

Posted 20 March 2012 - 04:03 AM

View Postpbl, 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

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?

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

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

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

View PostTsukuyomi, 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

Was This Post Helpful? 0
  • +
  • -

#6 Tsukuyomi  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 98
  • Joined: 22-February 11

Re: How to check if array indexes are empty and if so check the next?

Posted 20 March 2012 - 05:22 AM

View Postpbl, 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

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

View PostTsukuyomi, 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]);
    }

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: How to check if array indexes are empty and if so check the next?

Posted 20 March 2012 - 01:41 PM

View PostTsukuyomi, on 20 March 2012 - 08:22 AM, said:

on here:

for(int s=0; s<newData.length; s++){
        System.out.printf("%5d%8d\n", s, newData[s]);
    }

What "on here" ?
Was This Post Helpful? 0
  • +
  • -

#8 Tsukuyomi  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 98
  • Joined: 22-February 11

Re: How to check if array indexes are empty and if so check the next?

Posted 21 March 2012 - 06:30 PM

View Postpbl, on 20 March 2012 - 01:41 PM, said:

View PostTsukuyomi, on 20 March 2012 - 08:22 AM, said:

on here:

for(int s=0; s<newData.length; s++){
        System.out.printf("%5d%8d\n", s, newData[s]);
    }

What "on here" ?


If the index is not 0 how do check for the next index and if that one is not 0 how do i check the next one, etc?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1