Adblock breaks this site

Changing the size of an array

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

  1. DtheK

    DtheK Apprentice
    Banned

    Joined:
    Oct 17, 2009
    Posts:
    873
    Referrals:
    0
    Sythe Gold:
    0
    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?
     
  2. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    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);
     
  3. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    Changing the size of an array

    Vectors are perfect if you plan to resize an array.
     
  4. runeowner

    runeowner Forum Addict
    Banned

    Joined:
    Jan 23, 2007
    Posts:
    456
    Referrals:
    0
    Sythe Gold:
    0
    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 >


 
 
Adblock breaks this site