[英]Array without size in Java
我試圖定義一個沒有特定大小的數組,但似乎無法弄清楚。
感謝您提供任何幫助,有關我的編碼的任何評論都很棒,因此我可以解決該問題以供將來學習:)另外,count變量似乎可以計數64個字符,而不是txt文件中的27個字符?
謝謝。
編輯:僅供參考,還有一個類包含一些我目前正在使用的方法(在代碼末尾提到的MapFunctions
)
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Scanner;
public class MapMain {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String T;
System.out.print("What is the file dest?");
T=input.nextLine();
//String [][] map = new String[!][!];
InputStream is = null;
int i;
char c;
try{
// new input stream created
is = new FileInputStream(T);
System.out.println("Characters printed:");
int count;
count = 0;
// reads till the end of the stream
while((i=is.read())!=-1){
count=count+1;
c=(char)i;
// prints character
System.out.print(c);
for (int i = 0; i<array1.length;i++){
for (int j = 0; j<array1[i].length;j++){
array1[i][j]=c;
}
}
}
System.out.print("\n"+count+"\n");
}catch(Exception e){
// if any I/O error occurs
e.printStackTrace();
}finally{
}
MapFunctions ankosh = new MapFunctions();
}
}
數組必須具有固定的大小。 如果您事先不知道大小,請改用ArrayList
。
將值添加到ArrayList
,可以創建一個數組(其大小基於ArrayList
中元素的數量)並將數據復制到該ArrayList
。
如果不確定該用戶ArrayList,則Array應該聲明了行數。
在Java中,沒有大小或可變大小的數組就無法創建數組。 如果不確定大小,最好的方法是使用ArrayList而不是Array。 但是,如果您只想使用數組,那么您可以做的就是在數組大小已滿時將數組大小增加一些
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.