Results 1 to 8 of 8

Thread: Arrays

  1. #1
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default Arrays

    Hi,

    This is a program to sort the numbers in num array. This program does work, but I want to enter new values to the array num every time during execution. How do we give input to an array?

    import java.util.*;

    public class sort{
    public static void main(String[] args){
    int num[] = {50,20,45,82,25,63};
    int i,j,t;
    System.out.println("\n");
    System.out.print("Accending order of numbers : " + num[i] );
    Arrays.sort(num);
    for(i = 0;i <num.lengthl;i++){
    System.out.print(" " + num[i]);
    }
    }
    }

    Regards,
    Sarah

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Do you mean you want to declare the array like this
    Java Code:
    int[] anIntArr = new int[10];
    And fill it with random numbers in each spot? If you do, it's pretty simple, after declaring an array as I have done above you can loop through it and assign each item in the array with =.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Remember that arrays have a fixed length. Once created (and filled) you cannot add another value to it. You have to create another larger array, copy over all values and then add the new value.

    Then again it is not clear what you are trying to do.

  4. #4
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    No, for example the first time I am executing this program I want to enter 50,20,45,82,25,63 as input for sorting into num array and the other time time I execute I want to enter something like 60,46,29,78,39 into num array and when I execute again I want to enter some other numbers

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Ahhh, in that case do not hard code the values. Either pass in the values as command line arguments (which go to the args array) or get user input.

  6. #6
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Is it no possible to do in some other way other than passing the values as cmmand line arguments??

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I do believe I gave 2 options in my previous post.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Look up the scanner API, that and a loop can create an array of a user defined size and let you fill it with items.

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  2. Arrays
    By PhQ in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 09:40 PM
  3. using arrays
    By monikat in forum New To Java
    Replies: 3
    Last Post: 04-17-2010, 12:47 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. Help with arrays!
    By lilac87 in forum New To Java
    Replies: 2
    Last Post: 07-28-2009, 05:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •