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 2 of 2

Thread: how to add a new entry in a current array

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default how to add a new entry in a current array

    a liitle bit of program that demonstrates how to add a new entry in a current array that has current values

    public class ArraySampleExtendingIndex {
     
        private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
        public static void main(String[] args) throws IOException {
     
            boolean insertNew = false,
                    newEntry;
     
            int[] numbers = {10, 2},
                  temp;
     
            int length = numbers.length;
     
            System.out.print("The Current Values Are: ");
            System.out.print("[");
     
            // display the current values
            for (int x = 0; x <= numbers.length - 1; x++) {
     
                System.out.print(numbers[x]);
     
                if (x < numbers.length - 1) {
     
                    System.out.print(", ");
                }
            }
     
            System.out.print("]");
            System.out.print("\n\n");
     
            do {
     
                System.out.print("Add New Entry ? ");
                String response = br.readLine();
     
                if (response.equalsIgnoreCase("Y")) {
     
                    System.out.print("Enter Your New Entry: ");
                    int entry = Integer.parseInt(br.readLine());
     
                    temp = new int[length];
                    newEntry = true;
     
                    // assign the values in the temporary array corresponding to its last position
                    for (int x = 0 ; x <= length - 1; x++) {
     
                        temp[x] = numbers[x];
                    }
     
                    // make an update to the value of the length, dont just add 1
                    length++;
     
                    numbers = new int[length];
     
                    // add the new entry in the last index
                    for (int i = length - 1; i >= 0; i--) {
     
                        if (newEntry == true) {
     
                            numbers[i] = entry;
                            newEntry = false;
                        }
                        else {
     
                            numbers[i] = temp[i];
                        }
                    }
     
                    System.out.print("\n");
                    System.out.println("New Entry Has Been Added.");
                    System.out.print("\n");
     
                    insertNew = true;
                }
                else {
     
                    insertNew = false;
                }
     
            }
            while (insertNew);
     
            System.out.print("\n");
     
            System.out.print("Current Value Are: ");
            System.out.print("[");
     
            // display the array together with the added values
            for (int x = 0; x <= numbers.length - 1; x++) {
     
                System.out.print(numbers[x]);
     
                if (x < numbers.length - 1) {
     
                    System.out.print(", ");
                }
            }
     
            System.out.print("]");
            System.out.print("\n");
        }
    }

  2. The Following User Says Thank You to chronoz13 For This Useful Post:

    JavaPF (December 28th, 2009)


  3. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to add a new entry in a current array

    FYI, it is much more efficient to allocate an array with double the current size when the limit is reached. This is because each time you allocate a new array, you have to first find the memory space for that array and allocate it, then re-populate the array with the values in the original array, a very time consuming process. I believe this is actually how the ArrayList and Vector classes increase their size (or, it may be some other function that's greater than 1).

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    chronoz13 (December 28th, 2009)

Similar Threads

  1. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  2. Program to print current directory path to the console
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: October 9th, 2009, 12:59 PM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  4. How to Get the current date and time
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM