• Log in or sign up for an account here: https://www.sythe.org/login

Changing the size of an array

Joined
Oct 17, 2009
Sythe Gold
0
Messages
873
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?
 
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);
 
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);

Vectors are perfect if you plan to resize an array.
 
I think an ArrayList can change size? If not then you would have to create another Array with the modified size...
 
duelarena.gg
Back
Top