Changing the size of an array

Discussion in 'Programming General' started by DtheK, Dec 17, 2009.

Changing the size of an array
  1. Unread #1 - Dec 17, 2009 at 9:13 PM
  2. DtheK
    Joined:
    Oct 17, 2009
    Posts:
    873
    Referrals:
    0
    Sythe Gold:
    0

    DtheK Apprentice
    Banned

    Changing the size of an array

    Hi, I've recently started working on this programming problem and it requires one to change the size of an array.

    The program has you remove all the zeroes from the sequence, as well as all the digits from the left.

    Here is my code so far:

    Code:
    [I]import java.util.*;
    import java.util.StringTokenizer;
    import javax.swing.JOptionPane;
    public class ACSLDigDel
    {
    	public static void main(String[] args)
    	{
    		int n=0;
    		
    		String s = JOptionPane.showInputDialog("Enter the numbers seperated by a comma:");
    		
    		StringTokenizer st1 = new StringTokenizer(s, ",");
    		n=Integer.parseInt(st1.nextToken());
    		int num[]=new int[n];
    		
    		for(int m=0;m<n;m++)
    			num[m] = Integer.parseInt(st1.nextToken());
    			
    		for(int m=0;m<n;m++)
    		{
    			if(num[m]==0)
    			{
    				???
    			}
    		}
    	}
    }[/I]
    Is there any way to do this?
     
  3. Unread #2 - Dec 17, 2009 at 11:22 PM
  4. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Changing the size of an array

    Arrays cannot change size, so you would need to create a new array and copy all of the data into it.

    What you're looking for is a class to do this for you. Look into Collections (Vectors, ArrayLists, LinkedLists, etc). They are slower then arrays, but they manage the resizing without your input. You cal also use the toArray() method to convert the data in the collection into an array. Also, you can't use primitive types with generics, so use the wrapper classes found within the java.lang package (i.e. java.lang.Float as opposed to float);
     
  5. Unread #3 - Dec 17, 2009 at 11:28 PM
  6. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    Changing the size of an array

    Vectors are perfect if you plan to resize an array.
     
  7. Unread #4 - Dec 19, 2009 at 9:27 PM
  8. runeowner
    Joined:
    Jan 23, 2007
    Posts:
    456
    Referrals:
    0
    Sythe Gold:
    0

    runeowner Forum Addict
    Banned

    Changing the size of an array

    I think an ArrayList can change size? If not then you would have to create another Array with the modified size...
     
< Charny Autoclicker | A method for closing form1 then opening form2 >

Users viewing this thread
1 guest


 
 
Adblock breaks this site