如何检查是否数组索引是空的,如果是的话检查下? [英] How to check if array indexes are empty and if so check the next?

查看:437
本文介绍了如何检查是否数组索引是空的,如果是的话检查下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人有我如何能检查数组索引(不只是一个索引)是空的任何想法,如果是空的或零投入的价值在那里。而且,如果所有的指标均不是空的打印错误。

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.

对不起不幸的是我不能给代表。

Sorry unfortunately I can't give rep.

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

在这里:

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

如何检查多个索引(如果是空的?
如果索引是空的怎么检查下一个索引,如果一个不为空,我如何检查下一个,等等?

how do check for more than one index(if empty? 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?

推荐答案

在基本数组元素不能为空。他们将永远得到初始化的东西。

Elements in primitive arrays can't be empty. They'll always get initialized to something

如果您声明数组像这样

 int [] newData = new int [17];

,那么所有的元素将默认为零。

then all of the elements will default to zero.

有关检查,如果没有输入的元素,你可以使用一个简单的循环:

For checking if an element is not entered, you can use a simple loop :

 for(int i=0;i<newData.length;i++)
    {
        if(newData[i]==0)
            System.out.println("The value at " + i + "is empty");
    }

虽然,上述code不会工作你的情况,因为用户可能输入0作为输入值,仍然这code会认为它是空的。

Although , the above code will not work in your case, because the user might enter 0 as an input value and still this code will consider it to be empty.

你可以做的是,初始化所有值的数组为-1,并且在只值> = 0可输入输入提示指定。
初始化可以这样做:

What you can do is, initialize the array with all values as -1, and specify at the input prompt that only values >=0 can be entered . The initialization can be done like this:

int[] newData = new int[17];
for(int i=0;i<newData.length;i++)
{
   newData[i]= -1;   
}

然后,你可以要求用户输入并做处理。那么你可以使用这样的:

Then you can ask the user for input and do the processing. Then you can use this:

for(int i=0;i<newData.length;i++)
    {
        if(newData[i]==-1)
           System.out.println("The value at " + i + "is empty");
    }

这篇关于如何检查是否数组索引是空的,如果是的话检查下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆