我正在编写一个程序,将所有文件的内容复制到最后一个输入文件(所有文件都由用户在命令提示符下给出)。那么,我该如何声明一个数组,该数组需要用户的大小呢?在运行时声明数组的大小
-5
A
回答
9
那样做:
import java.util.Scanner; <-------- import the class
public class InputFromUser {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number for the array size:");
int size = scan.nextInt();
int[] arr = new int[size];
}
}
要么使用java.io
这样的:
BufferedReader columnInput = new BufferedReader(new InputStreamReader (System.in));
int size = Integer.parseInt(columnInput.readLine());
0
如果提供的文件列表中运行的应用程序作为参数时:你可以使用它返回所提供的参数数量args.length属性:
public static void main(String[] args) {
System.out.println(args.length);
}
+0
这好好尝试回答任何方面的问题。 –
+0
@sᴜʀᴇsʜᴀᴛᴛᴀ这个问题非常模糊。我认为这是一个很好的答案,但chenchuk解释了这个问题与你的做法不同。我认为惩罚答辩人回答措辞不佳的问题是错误的。 –
+0
为什么不呢?命令行提供的文件列表作为参数,这应该是数组大小...不是它的问题吗? – chenchuk
0
你也可以做这种方式:
public static void main(String args[]) {
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
try {
System.out.println("Enter a array size:");
int arraysize = Integer.parseInt(input.readLine());
int[] arr = new int[arraysize];
} catch (Exception e) {
e.printStackTrace();
}
}
相关问题
- 1. 数组大小声明
- 2. 串联在COUT声明数组大小
- 3. 在Visual Basic中声明数组大小
- 4. 声明未知大小的数组
- 5. 声明大小声明为extern的数组const
- 6. 声明大小为STD数组:: streamoff
- 7. 运行时大小的数组
- 8. 如何在C++中声明不同大小的数组数组?
- 9. 如何在运行时在Vb.net中声明数组
- 10. C++声明不知道大小的数组数组
- 11. 如何在C++中声明一个数组的固定大小
- 12. 试图在Scala中声明一定大小的数组
- 13. 在堆栈上声明固定大小的字符数组C++
- 14. 在C++中声明数组大小的最佳方法
- 15. 在codeigniter中声明一个大小为100的空数组php
- 16. 如何在C中声明一个可变大小的数组?
- 17. 在动态大小声明后在fortran中定义数组
- 18. 在C++中声明大字符数组
- 19. 可以在运行时确定数组的大小c?
- 20. 如何在运行时定义大小的数组?
- 21. 如何在运行时分配二维数组的大小?
- 22. 如何在运行时分配Netcdf fortran数组的大小?
- 23. 用c语言在运行时确定大小的数组?
- 24. 如何在python(或特定大小的数组)中声明零数组
- 25. 在运行时声明类成员D
- 26. 在运行时声明常量(Java)?
- 27. 在运行时声明属性?
- 28. 声明数组属性的大小的类型定义
- 29. 数组大小的声明是非法的
- 30. Arduino的可变大小的数组声明
'字符串文件名[] =新的String [lenghtTakenByUserFromConsoleWhichIsValidInteger];' –