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

前端 未结 7 634
庸人自扰
庸人自扰 2021-02-04 03:47

I\'m asking the user to enter some numbers between 1 and 100 and assign them into an array. The array size is not initialized since it is dependent on the number of times the us

相关标签:
7条回答
  • 2021-02-04 03:57

    **input of list of number for array from single line.

       String input = sc.nextLine();
        String arr[] = input.split(" ");
        int new_arr[] = new int[arr.length];
        for(int i=0; i<arr.length; i++)
        {
             new_arr[i] = Integer.parseInt(arr[i]);
        }
    
    0 讨论(0)
  • 2021-02-04 04:00

    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]);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-04 04:00
    int i,largest = 0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the number of numbers in the list");
    i = scan.nextInt();
    int arr[] = new int[i];
    System.out.println("Enter the list of numbers:");
    for(int j=0;j<i;j++){
        arr[j] = scan.nextInt();
    }
    

    The above code works well. I have taken the input of the number of elements in the list and initialized the array accordingly.

    0 讨论(0)
  • 2021-02-04 04:01

    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.

    0 讨论(0)
  • 2021-02-04 04:08

    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.

    0 讨论(0)
  • 2021-02-04 04:17
    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.

    0 讨论(0)
提交回复
热议问题