0

I'm trying to define an array without any specific size but cannot seem to figure it out.

Any help is appreciated and any comments regarding my coding is awesome so I could fix that for future learning :) Also, the count variable seems to count 64 characters instead of the 27 in the txt file?

Thanks.

EDIT: FYI there is another class that contains some methods I'm currently working on (which is MapFunctions mentioned at the end of the code)

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();
    }
}
1
  • 3
    You can't, an array MUST have a fixed size. You can copy it to a larger or smaller array should you need to or you can use the inbuilt collections API, including ArrayList, which is a dynamically resizable array or a HashMap depending on your needs Commented Oct 6, 2014 at 8:22

3 Answers 3

4

An array must have a fixed size. If you don't know the size in advance, use an ArrayList instead.

After you finish adding the values to the ArrayList, you can create an array (whose size is based on the number of elements in the ArrayList) and copy the data to it.

Sign up to request clarification or add additional context in comments.

4 Comments

is it possible to have a 2D ArrayList? and can i specify the element i want to remove ( As in map[row][col]) ? @Eran
@Ankosh You can have an ArrayList<ArrayList<SomeType>>.
is it used right in the following example i just wrote? ArrayList<ArrayList<String>> Contain = new ArrayList<ArrayList<String>>(); Contain.get(1(1)); @Eran
@Ankosh no. It should be Contain.get(1).get(1).
0

Array should have number of rows declared, If you are not sure of that user ArrayList.

You can read example here

2 Comments

In the example is says getting the element by index, is it possible to have a 2D ArrayList?
You can insert Object in ArrayList, you can also insert String array in ArrayList. so it will work like a 2D string Array and you wont need to set the size in advance.
0

You can't create arrays without size or variable size arrays in Java. If you are not sure about the size the best approach is to use ArrayList instead of Array. But if you want to use only arrays then you what you can do is increase the array size by some factor when the array size if full

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.