使用一维数组编写 Java 程序。有一个包含 10 个数字的列表并根据索引获取一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17689877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a Java program using single dimensional array. Have a list of 10 numbers and fetch a value based on the index
提问by Programming Noob
So I have this assignment for my Programming class, and I'm lost on what to do for the second part. This is the code I have so far:
所以我的编程课有这个作业,我不知道第二部分该做什么。这是我到目前为止的代码:
class SingleArray
{
public static void main(String[]args)
{
int[] b=new int[]{5,10,15,20,25,30,35,40,45,50};
Not much I know, but I really am confused on how I would get it to fetch the different array values. Do I have to import the util scanner to do this? Any help to point me in the right direction? Thanks guys!
我知道的不多,但我真的很困惑如何让它获取不同的数组值。我是否必须导入 util 扫描程序才能执行此操作?任何帮助我指出正确的方向?多谢你们!
回答by Steve P.
To access an index i
in int[] b
, you use: b[i]
. For example:
要访问索引i
中int[] b
,您可以使用:b[i]
。例如:
System.out.println(b[i]); //prints the int at index i
b[i] = 5; //assigns 5 to index i
b[i] = x; //assigns an int x, to index i
int y = b[i]; //assigns b[i] to y
To search through the array for a number x
:
要在数组中搜索数字x
:
for (int i = 0; i < b.length; i++)
{
if (b[i] == x)
{
//doSomething
}
}
There are also auxiliary methods provided by the Java Arraysclass.
Java Arrays类还提供了一些辅助方法。
Here's a tutorial on arrays, here's an additional Java Language Basics tutorial.
这是关于数组的教程,这是附加的Java 语言基础教程。
回答by Sajal Dutta
Please do the assignments in the comments below-
请在下面的评论中完成任务-
class SingleArray {
// Declare an array of 10 integers.
// Assignment: Why did I declare it as static?
private static int[] array = new int[] { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
// A method that returns the value at a given index from the array
// Assignment: Why the method below is static?
private static int getValue(int index){
return array[index];
}
public static void main(String[] args){
for (int i = 0; i < array.length; i++){
int valueAtIndex = getValue(i);
System.out.println("Index: " + (i + 1) + " Value: " + valueAtIndex);
// Assignment: remove the statement "int valueAtIndex = getValue(i);" and
// modify the System.out.println statement to have the same result
}
}
}
回答by ihsan kocak
List<Integer> integerList=new ArrayList<Integer>();
for(int i=0;i<10;i++){
integerList.add(i);
}
//you can fetch the values by using this method:
integerList.get(valueIndex);
回答by Pratibh Acharya
This might help:
这可能有帮助:
public class OneDimensionalArrays {
public static void main(String[] args) {
//initialize an array num with the respective values
int []num={5,10,15,20,25,30,35,40,45,50};
// The looping will work within the range of the array.
for (int i = 0; i<num.length;i++){
System.out.println("Enter the index to find the value of ");
//To catch array index out of bound exception
try{
Scanner sc = new Scanner(System.in);
//Save the user inputted value in indexValue variable
int indexValue = sc.nextInt();
// Enter 100 to terminate the program execution
if (indexValue != 100) {
System.out.println("Value in the respective index of array is " + num[indexValue]);
}
else{
System.out.println("Program has been terminated by user");
break;
}
}
catch (Exception e ){
System.out.println("Enter the index within range of the array");
}
}
}
}