Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
索引帮助Java_Java_Arrays - Fatal编程技术网

索引帮助Java

索引帮助Java,java,arrays,Java,Arrays,我想知道是否有办法获得索引,让我给你举个例子 String[] names = {"Daniel", "Lewis", "Sarah", "John"}; String cmd = input.nextLine(); String CMD[] = cmd.split(" "); if (CMD[0].equalsIgnoreCase("my name is") && CMD[1].equalsIgnoreCase(accountIndex[0])){

我想知道是否有办法获得索引,让我给你举个例子

String[] names = {"Daniel", "Lewis", "Sarah", "John"};

   String cmd = input.nextLine();

  String CMD[] = cmd.split(" ");


  if (CMD[0].equalsIgnoreCase("my name is") && CMD[1].equalsIgnoreCase(accountIndex[0])){

     System.out.println("My name is Daniel!");

  } else if (CMD[0].equalsIgnoreCase("my name is") && CMD[1].equalsIgnoreCase(accountIndex[1])) {

     System.out.println("My name is Lewis!");
  } else if (CMD[0].equalsIgnoreCase("my name is") && CMD[1].equalsIgnoreCase(accountIndex[2])) {

     System.out.println("My name is Sarah!");
  } else if (CMD[0].equalsIgnoreCase("my name is") && CMD[1].equalsIgnoreCase(accountIndex[3])) {

     System.out.println("My name is John!");
  }
有没有比嵌套if语句更简单的方法

请注意,我只想在表中使用名称,所以我不能让名为myName的字符串等于CMD[1]

我想这有点像一个用户名数据库,如果你的用户名不存在,你就不能登录


我希望这样,但是不嵌套if语句,并且names数组就是这个实例中的数据库

您可以尝试
for
循环:

for (int i = 0; i < names.length(); i++) {
    if (CMD[3].equalsIgnoreCase(accountIndex[i])) {
        System.out.println("My name is " + names[i] + "!");
        break;
    }
}
for(int i=0;i
您可以尝试
for
循环:

for (int i = 0; i < names.length(); i++) {
    if (CMD[3].equalsIgnoreCase(accountIndex[i])) {
        System.out.println("My name is " + names[i] + "!");
        break;
    }
}
for(int i=0;i
您可以将有效用户名存储在树集合中,然后当用户输入名称时,您可以查找树集合以测试给定名称是否有效

TreeSet<String> validNames = new TreeSet<String>();
validNames.add("John");
validNames.add("Mary");
.....
.....

// For searching
if(validNames.contains(CMD[0]))
{
 // name exists
}
else{
   // invalid name
}
TreeSet validNames=new TreeSet();
有效名称。添加(“约翰”);
有效名称。添加(“玛丽”);
.....
.....
//搜寻
if(validNames.contains(CMD[0]))
{
//名称存在
}
否则{
//无效名称
}
注意:您可以将名称存储在任何可搜索的集合中(对于此场景,哈希映射也是一个不错的选择。这只是一个指针,您需要深入挖掘,以找到适合您需要的数据结构。有关更多信息,您可以参考以下链接:


您可以将有效用户名存储在树集合中,然后当用户输入名称时,您可以查找树集合以测试给定名称是否有效

TreeSet<String> validNames = new TreeSet<String>();
validNames.add("John");
validNames.add("Mary");
.....
.....

// For searching
if(validNames.contains(CMD[0]))
{
 // name exists
}
else{
   // invalid name
}
TreeSet validNames=new TreeSet();
有效名称。添加(“约翰”);
有效名称。添加(“玛丽”);
.....
.....
//搜寻
if(validNames.contains(CMD[0]))
{
//名称存在
}
否则{
//无效名称
}
注意:您可以将名称存储在任何可搜索的集合中(对于此场景,哈希映射也是一个不错的选择。这只是一个指针,您需要深入挖掘,以找到适合您需要的数据结构。有关更多信息,您可以参考以下链接:

运行
cmd.split()
将拆分数组,得到一个数组:
{“my”、“name”、“is”、“some_name”}
。这意味着要检查的名称将在数组的第四个元素处给出,因此索引
[3]

String[] names = {"Daniel", "Lewis", "Sarah", "John"};

String cmd = input.nextLine();
String CMD[] = cmd.split(" ");

// Initial check to see if my name is exists.
if(cmd.subtstring(0, 10).equalsIgnoreCase("my name is") && cmd.length > 3)
{
    // This loop is better than checking each individual case because it allows you to dynamically add elements to your names array
    for(int i = 0; i < names.length; i++)
    {
        // Must start at the fourth element since my, name, and is will be the 0-2 elements.
        if(CMD[3].equalsIgnoreCase(names[i]))
        {
            System.out.println("My name is "+names[i]+"!");
            break;
        }
    }
}
String[]name={“丹尼尔”、“刘易斯”、“莎拉”、“约翰”};
String cmd=input.nextLine();
字符串CMD[]=CMD.split(“”);
//初步检查我的名字是否存在。
if(cmd.substring(0,10).equalsIgnoreCase(“我的名字是”)&&cmd.length>3)
{
//此循环优于检查每个单独的大小写,因为它允许您动态地将元素添加到名称数组中
for(int i=0;i
运行
cmd.split()
将拆分数组,得到一个数组:
{“my”、“name”、“is”、“some_name”}
。这意味着要检查的名称将在数组的第四个元素处给出,因此索引
[3]

String[] names = {"Daniel", "Lewis", "Sarah", "John"};

String cmd = input.nextLine();
String CMD[] = cmd.split(" ");

// Initial check to see if my name is exists.
if(cmd.subtstring(0, 10).equalsIgnoreCase("my name is") && cmd.length > 3)
{
    // This loop is better than checking each individual case because it allows you to dynamically add elements to your names array
    for(int i = 0; i < names.length; i++)
    {
        // Must start at the fourth element since my, name, and is will be the 0-2 elements.
        if(CMD[3].equalsIgnoreCase(names[i]))
        {
            System.out.println("My name is "+names[i]+"!");
            break;
        }
    }
}
String[]name={“丹尼尔”、“刘易斯”、“莎拉”、“约翰”};
String cmd=input.nextLine();
字符串CMD[]=CMD.split(“”);
//初步检查我的名字是否存在。
if(cmd.substring(0,10).equalsIgnoreCase(“我的名字是”)&&cmd.length>3)
{
//此循环优于检查每个单独的大小写,因为它允许您动态地将元素添加到名称数组中
for(int i=0;i
你可以这样做

List<String> names = Arrays.asList("Daniel", "Lewis", "Sarah", "John");
if (names.contains(CMD[3]) {
    System.out.println("My name is " + CMD[0]);
} else {
    // not found...
}
List name=Arrays.asList(“丹尼尔”、“刘易斯”、“莎拉”、“约翰”);
if(names.contains(CMD[3]){
System.out.println(“我的名字是”+CMD[0]);
}否则{
//找不到。。。
}
你可以这样做

List<String> names = Arrays.asList("Daniel", "Lewis", "Sarah", "John");
if (names.contains(CMD[3]) {
    System.out.println("My name is " + CMD[0]);
} else {
    // not found...
}
List name=Arrays.asList(“丹尼尔”、“刘易斯”、“莎拉”、“约翰”);
if(names.contains(CMD[3]){
System.out.println(“我的名字是”+CMD[0]);
}否则{
//找不到。。。
}

您可以将数组转换为列表,然后使用
contains
方法在列表中搜索名称:

if(Arrays.asList(names).contains(CMD[3]))
    System.out.println("My name is " + CMD[3] + "!");

您可以将数组转换为列表,然后使用
contains
方法在列表中搜索名称:

if(Arrays.asList(names).contains(CMD[3]))
    System.out.println("My name is " + CMD[3] + "!");

你在一个空间上分裂,所以

CMD[0].equalsIgnoreCase("my name is")
将永远不会评估为真

你为什么要测试“我的名字是”
?如果你使用的是第四个单词的格式,那就捕获它:
CMD[3]

String[] names = {"Daniel", "Lewis", "Sarah", "John"};

String cmd = input.nextLine();
String CMD[] = cmd.split(" ");

// Initial check to see if my name is exists.
if(cmd.subtstring(0, 10).equalsIgnoreCase("my name is") && cmd.length > 3)
{
    // This loop is better than checking each individual case because it allows you to dynamically add elements to your names array
    for(int i = 0; i < names.length; i++)
    {
        // Must start at the fourth element since my, name, and is will be the 0-2 elements.
        if(CMD[3].equalsIgnoreCase(names[i]))
        {
            System.out.println("My name is "+names[i]+"!");
            break;
        }
    }
}
要避免这些if-else-if-else,请将名称放入一个映射中,其中键是名称,值始终为
null

然后根据键测试名称:

if(mapOfNames.containsKey(CMD[3]))

你在一个空间上分裂,所以

CMD[0].equalsIgnoreCase("my name is")
将永远不会评估为真

你为什么要测试“我的名字是”
?如果你使用的是第四个单词的格式,那就捕获它:
CMD[3]

String[] names = {"Daniel", "Lewis", "Sarah", "John"};

String cmd = input.nextLine();
String CMD[] = cmd.split(" ");

// Initial check to see if my name is exists.
if(cmd.subtstring(0, 10).equalsIgnoreCase("my name is") && cmd.length > 3)
{
    // This loop is better than checking each individual case because it allows you to dynamically add elements to your names array
    for(int i = 0; i < names.length; i++)
    {
        // Must start at the fourth element since my, name, and is will be the 0-2 elements.
        if(CMD[3].equalsIgnoreCase(names[i]))
        {
            System.out.println("My name is "+names[i]+"!");
            break;
        }
    }
}
要避免这些if-else-if-else,请将名称放入一个映射中,其中键是名称,值始终为
null

然后根据键测试名称:

if(mapOfNames.containsKey(CMD[3]))

accountIndex将被Names取代。你能提供更多关于你想要完成的任务的信息吗?当然,如果你想知道我到底想用它做什么,它就像一个电子邮件系统。你可以这样做!从Table texthere发电子邮件给somenamefromtable,如果名称有效,它会说“电子邮件发送到[name from Array],那么文本可以是”这是一个测试“什么是会计?”