Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 28-12-2009, 01:48 PM
chronoz13's Avatar
Java kindergarten
 

Join Date: Mar 2009
Location: Squatters Mansion
Posts: 405
Thanks: 113
Thanked 21 Times in 21 Posts
chronoz13 is on a distinguished road

I'm feeling Stressed
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

Java Code
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");
    }
}



Reply With Quote Share this thread on Facebook
The Following User Says Thank You to chronoz13 For This Useful Post:
JavaPF (28-12-2009)
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 28-12-2009, 10:39 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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).
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
The Following User Says Thank You to helloworld922 For This Useful Post:
chronoz13 (29-12-2009)
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Object array to int array? rsala004 Collections and Generics 1 30-10-2009 08:09 AM
How to Get the current directory path JavaPF Java Code Snippets and Tutorials 1 09-10-2009 05:59 PM
Storing an array into an array vluong Collections and Generics 4 22-09-2009 07:14 PM
How to Get the current date and time JavaPF Java Code Snippets and Tutorials 2 02-12-2008 05:55 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jbutton java jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:50 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.