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

Thread: Expanding the capacity of an array

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Expanding the capacity of an array

    Hello all,
    I declare an array of objects with a default capacity of 2 and I am trying to expand capacity once the limit is meet. I am calling the "expandCapacity" method which creates a new array of twice the old size and copies the old array into the new one. I get the following error:

    "Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LcharFreqNode;
    at driver.main(driver.java:45)"

    function call from main
     
    if (count >= mycharFreqNode.length)
    	    	  mycharFreqNode = arrayOps.expandCapacity(mycharFreqNode);

    expandCapcity method
    public class arrayOps<T> {
     
    	public static <T> T[] expandCapacity(T[] data){
    		T[] larger = (T[]) (new Object [data.length *2]);
     
    		for (int index = 0; index < data.length; index++)
    			larger[index] = data[index];
     
    		data = larger;
    		return larger;
    		}
    }


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

    Default Re: Expanding the capacity of an array

    What is the type of myCharFreqNode?

    Generics can't parametrize java primitive values, you must use the wrapper class.

    int[] primitive = new int[5];
    Integer[] wrapper = new Integer[5];
    expandCapacity(primitive); // error
    expanceCapacity(wrapper); // ok

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Expanding the capacity of an array

    HW your comment is true, but using a integer will not work. Creating a new array of size Object doesn't contain enough space for an array of type Integer.

    C makes tis sort of thing easy


    Chris

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Expanding the capacity of an array

    Thank you for the replies.
    myCharFreqNode is the type of an object I created. It is declared like this in main

    charFreqNode mycharFreqNode[] = new charFreqNode[2];

    The class definition of charFreqNode is below:

    public class charFreqNode {
     
    	//class data members
    	char myChar;
    	int myCharFreq;
     
    	//constructor
    	public <T> charFreqNode(T myChar, T myCharFreq)
    	{
    		this.myChar = (Character) myChar;
    		this.myCharFreq = (Integer) myCharFreq;
    	}
     
     
    	public int compareTo(charFreqNode myNode)
    	{
    		int result = 0;
     
    		if (this.myCharFreq < myNode.myCharFreq)
    			result = -1;
     
    		return result;
    	}
    	public String toString(int count)
    	{
    		String result = null;
    		result = String.format( "Node #%d has character \"%c\" with a frequency \"%d.\"", count, this.myChar, this.myCharFreq);
    		return result;
     
    	}
     
    }

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

    Default Re: Expanding the capacity of an array

    Ahh... I was only paying attention to compile errors

    Yeah, you're casting an Object array into a different kind of array (a no-no).

    Unfortunately, there's no way to create a generic array in Java. You have two options:

    1. Use an Object array (note: this is not a good solution unless you're checking that only a certain type is being added to that array)
    2. Use an ArrayList. This functions as an array (indeed, it uses an array for data storage), and will auto-expand as elements are being added. It does have support for generic arguments.

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Expanding the capacity of an array

    Because arrays are fixed size, the rule of thumb is to only use arrays when you know in advance exactly how big they will need to be. For dynamic arrays, use ArrayList (or Vector if multi-threading).

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM