如何根据输入的信息(即名称)创建数组,然后将其用作代码中的对象?

时间:2022-12-06 07:40:07

I've been reading up on it, but every question I've found has asked for slightly different things, such as only wanting a single letter for their array, or in a different language (I'm new and only learning java at the moment), so here I am.

我一直在阅读它,但我发现的每一个问题都要求稍微不同的东西,例如只想要一个单独的字母用于他们的数组,或者用另一种语言(我是新的,只学习java的那一刻),我在这里。

I want to set up an array that uses the user's input for their names.

我想设置一个使用用户输入作为名称的数组。

What I have so far is this, I'm assuming this is the declaration line, where later I use an input line to define a value within the array (which I also am unsure how to do)

到目前为止我所拥有的是,我假设这是声明行,后来我使用输入行来定义数组中的值(我也不确定该怎么做)

String[] array = {"name"};

But I don't know how to for example print.out the object or keep up with which name will be what value. I appreciate your time taken to teach me!

但我不知道如何打印。对象或跟上哪个名称将是什么价值。我很感谢你花时间教我!

EDIT for further clarification. I'm trying to write up a small app that asks the user for numerous names, addresses, and phone numbers (Type name -> Type name's address -> type name's phone number, ask if they want to add another person, if yes then go back to asking for another name)

编辑进一步澄清。我正在尝试编写一个小应用程序,询问用户多个姓名,地址和电话号码(类型名称 - >类型名称的地址 - >类型名称的电话号码,询问他们是否要添加其他人,如果是,则回去问另一个名字)

I am unsure how to set up a String array or how to use it throughout. However, thanks to your input and coming back after some fresh air, I have a better idea how to word it for google. Thank you guys for your help, even if it was just to gesture a better articulated question.

我不确定如何设置一个String数组或如何在整个过程中使用它。但是,感谢您的投入,并在一些新鲜空气后回来,我有一个更好的想法如何为谷歌。谢谢你们的帮助,即使只是为了表达一个更好的明确问题。

1 个解决方案

#1


0  

An array is a sequence of values. You have created an array of Strings that is one String long. To access the value at a specific of an array, use array subscript notation: the name of the array followed by a pair of square brackets ([]) with the index in between them.

数组是一系列值。您已创建一个字符串数组,其长度为一个字符串。要访问特定数组的值,请使用数组下标表示法:数组的名称后跟一对方括号([]),其中索引位于它们之间。

String[] anArrayOfStrings = {"string0", "string1", "string2"};

anArrayOfStrings[0]; //the first element

System.out.println(anArrayOfStrings[1]); //print the second element

anArrayOfStrings[2] = "new string value"; //assign the third element to a new value

if (anArrayOfStrings[0].equals("string0") //evaluate the first element and call a method
{
     //this block will execute anArrayOfStrings[0] is "string0"
} 

anArrayOfStrings[3]; //error, index out of bounds

Simply declaring the array would be

简单地声明阵列就可以了

String[] names;

In your code you both declare and assign it in the same line by using an initializer list. To assign individual elements, use the [] notation. Note that once you initialized you list to be only one String long, it cannot become longer than without be re-assigned. To declare an array of any size, you can use:

在您的代码中,您都使用初始化列表声明并在同一行中分配它。要分配单个元素,请使用[]表示法。请注意,一旦初始化列表只有一个字符串长,它不会变得比没有重新分配更长。要声明任何大小的数组,您可以使用:

 String[] arrayWithInitialSize = new String[5]; //holds five strings, each null to begin with

#1


0  

An array is a sequence of values. You have created an array of Strings that is one String long. To access the value at a specific of an array, use array subscript notation: the name of the array followed by a pair of square brackets ([]) with the index in between them.

数组是一系列值。您已创建一个字符串数组,其长度为一个字符串。要访问特定数组的值,请使用数组下标表示法:数组的名称后跟一对方括号([]),其中索引位于它们之间。

String[] anArrayOfStrings = {"string0", "string1", "string2"};

anArrayOfStrings[0]; //the first element

System.out.println(anArrayOfStrings[1]); //print the second element

anArrayOfStrings[2] = "new string value"; //assign the third element to a new value

if (anArrayOfStrings[0].equals("string0") //evaluate the first element and call a method
{
     //this block will execute anArrayOfStrings[0] is "string0"
} 

anArrayOfStrings[3]; //error, index out of bounds

Simply declaring the array would be

简单地声明阵列就可以了

String[] names;

In your code you both declare and assign it in the same line by using an initializer list. To assign individual elements, use the [] notation. Note that once you initialized you list to be only one String long, it cannot become longer than without be re-assigned. To declare an array of any size, you can use:

在您的代码中,您都使用初始化列表声明并在同一行中分配它。要分配单个元素,请使用[]表示法。请注意,一旦初始化列表只有一个字符串长,它不会变得比没有重新分配更长。要声明任何大小的数组,您可以使用:

 String[] arrayWithInitialSize = new String[5]; //holds five strings, each null to begin with