Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: array numbers

  1. #1
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default array numbers

    Hello,
    For my Java class I have to create an array. I won't know how many numbers will be input into the array. So far on the youtube videos I've seen, to create an array, they always seem to know how many numbers their want to store. So their examples are

    Double nameOfArray[] = new Double[10];

    so the array named nameOfArray will be an array using the Double type of number, and will have 10 values.

    I have to create an array in which I don't know how many values, because the user will be inputting values from ( I assume ) the Scanner. For now to get around this, my array looks like

    Double salesForWeek[] = new Double[900000];

    Is there a better way to make this array? Also, I'm assuming I would need to use the Scanner since I need to prompt the user for the number. Is that a correct assumption?
    Any help will be greatly appreciated.

  2. #2
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: array numbers

    For dynamically sized array, you could use ArrayList:

    import java.util.Scanner;
    import java.util.ArrayList;
     
    class MyArray {
    	public static void main(String[] args) {
    		ArrayList<Integer> array = new ArrayList<Integer>();
    		Scanner buf = new Scanner(System.in);
    		int input;
    		while(buf.hasNextInt()) {	//enter non-integer to exit loop
    			input = buf.nextInt();
    			array.add(input);	// use array.add() to append item to array 
    		}
    		System.out.println("Size of array is " + array.size());
    		for(int i=0; i<array.size(); i++) {
    			// use array.get(int index) instead of array[int index]
    			System.out.println("array[" + i + "] = " + array.get(i));
    		}
     
    	}
    }

    ArrayList documentation

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: array numbers

    You can also prompt for an array size and then use that in your declaration for the array. However, unless you are required to use arrays, the best method is what dboxall123 said. Use ArrayList.

    Regards,
    Jim

  4. #4
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: array numbers

    Awesome! thank you some much for the help. I'll try the ArrayList.

Similar Threads

  1. Replies: 4
    Last Post: February 10th, 2013, 11:58 PM
  2. Printing out numbers from an Array.
    By djl1990 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 13th, 2011, 12:03 PM
  3. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  4. selecting even numbers from an array
    By Tate in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 29th, 2010, 08:03 AM
  5. finding specified NUMBERS in an array
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 10:35 PM