Extend the size of an array


public class Main {
    
    /**
     * Extends the size of an array.
     */
    public void extendArraySize() {
        
        
        String[] names = new String[] {"Joe", "Bill", "Mary"};
        
        //Create the extended array
        String[] extended = new String[5];
        
        //Add more names to the extended array
        extended[3] = "Carl";
        extended[4] = "Jane";
        
        //Copy contents from the first names array to the extended array
        System.arraycopy(names, 0, extended, 0, names.length);
        
        //Ouput contents of the extended array
        for (String str : extended) 
            System.out.println(str);
        
    }
    /**
     * Starts the program
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().extendArraySize();
    }
}

0 comments:

                                                                

Site Meter