Java: how do I initialize an array size if it's unknown?

ID : 274495

viewed : 38

Tags : javajava





Top 5 Answer for Java: how do I initialize an array size if it's unknown?

vote vote

91

You should use a List for something like this, not an array. As a general rule of thumb, when you don't know how many elements you will add to an array before hand, use a List instead. Most would probably tackle this problem by using an ArrayList.

If you really can't use a List, then you'll probably have to use an array of some initial size (maybe 10?) and keep track of your array capacity versus how many elements you're adding, and copy the elements to a new, larger array if you run out of room (this is essentially what ArrayList does internally). Also note that, in the real world, you would never do it this way - you would use one of the standard classes that are made specifically for cases like this, such as ArrayList.

vote vote

84

I think you need use List or classes based on that.

For instance,

ArrayList<Integer> integers = new ArrayList<Integer>(); int j;  do{     integers.add(int.nextInt());     j++; }while( (integers.get(j-1) >= 1) || (integers.get(j-1) <= 100) ); 

You could read this article for getting more information about how to use that.

vote vote

78

I agree that a data structure like a List is the best way to go:

List<Integer> values = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int value; int numValues = 0; do {     value = in.nextInt();     values.add(value); } while (value >= 1) && (value <= 100); 

Or you can just allocate an array of a max size and load values into it:

int maxValues = 100; int [] values = new int[maxValues]; Scanner in = new Scanner(System.in); int value; int numValues = 0; do {     value = in.nextInt();     values[numValues++] = value; } while (value >= 1) && (value <= 100) && (numValues < maxValues); 
vote vote

64

If you want to stick to an array then this way you can make use. But its not good as compared to List and not recommended. However it will solve your problem.

import java.util.Scanner;  public class ArrayModify {      public static void main(String[] args) {         int[] list;         String st;         String[] stNew;         Scanner scan = new Scanner(System.in);         System.out.println("Enter Numbers: "); // If user enters 5 6 7 8 9          st = scan.nextLine();         stNew = st.split("\\s+");         list = new int[stNew.length]; // Sets array size to 5          for (int i = 0; i < stNew.length; i++){             list[i] =  Integer.parseInt(stNew[i]);             System.out.println("You Enterred: " + list[i]);         }     } } 
vote vote

59

String line=sc.nextLine();     int counter=1;     for(int i=0;i<line.length();i++) {         if(line.charAt(i)==' ') {             counter++;         }     }     long[] numbers=new long[counter];     counter=0;     for(int i=0;i<line.length();i++){         int j=i;         while(true) {             if(j>=line.length() || line.charAt(j)==' ') {                 break;             }             j++;         }         numbers[counter]=Integer.parseInt(line.substring(i,j));         i=j;         counter++;       }     for(int i=0;i<counter;i++) {         System.out.println(numbers[i]);     }     

I always use this code for situations like this. beside you can recognize two or three or more digit numbers.

Top 3 video Explaining Java: how do I initialize an array size if it's unknown?







Related QUESTION?