removing a number and returning the new array

we have to show the array and then remove and reshow the new array

Page 1 of 1

11 Replies - 2330 Views - Last Post: 21 November 2009 - 02:14 AM Rate Topic: -----

#1 eserr33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 09

removing a number and returning the new array

Post icon  Posted 20 November 2009 - 05:59 PM

//* This skeleton combines both parts of assignment 4 into one.  You can do the same
// * by Serrano

import java.util.*;

public class numbermove {
	//various methods used
	static Scanner input = new Scanner(System.in);
	public static void printArray(int[] array, int size)
	  {
			for(int i=0; i<size; i++) 
			{
				System.out.print(array[i] + " ");
			}
			System.out.println();
		}
	
	public static int getIndex(int[] array, int size, int index)
	{
		// this method searches for item in array and if found returns its index.  Otherwise returns -1
		for (int i=0; i<size; i++) 
		{
			if (array[i] == index)
			   return i;
		}
		return -1;
	}
	
	public static int removeAt(int[] array, int size, int item)
	{
		// removes from array whatever item is at given index and shifts remaining elements.
		//return the updated size
		if (item >= size) 
		{
			//print index out of bound.  Nothing removed.
			return size;
		}
		//otherwise shift elements to left from the cell whose item was removed
		for (int i =item; i<item-1; i++) {
			array[i] = array[i+1]; // value in cell i is overwritten by the value in cell i+1
		}
		return size--;
	}

	
	public static int remove(int[] array, int size, int item) 
	{
		//first find if the item is in the array by calling getIndex method
		int index = getIndex(array, size, item);
		if (index == -1) 
		{
			//print item not found, so nothing removed.
			System.out.println("nothing removed:\n");
			// without any changes to the size.
			return size;
			}
		else{
		
		return removeAt(array, size, item);	
	}
	}
		//otherwise call removeAt to remove that item and update the array and its size
public static void main(String[] args) 
{
	
	//declare		
	final int MAX_SIZE = 5;
	int[] array = new int[MAX_SIZE];
	int numericalOut;
	
	
	
	// prompt user as before to get values for an array
	   System.out.println("Please input data values as integers:\n");
	   for (int i=0; i<array.length; i++) 
		{
			array[i] = input.nextInt();
		}
		
	// print the array
		System.out.println(":/n");
		printArray(array,array.length);
		
	//prompt user for an integer to remove from the array and capture it as item.
		System.out.println("Please choose which integer too remove:/n");
		 numericalOut = input.nextInt();
		//call remove() method to remove that item.	 
		 remove(array, array.length, numericalOut);	
		
		//print the modified array	
		 System.out.println("" +
		 		"here is the up dated array:/n");
		 for (int i=0; i<array.length; i++)
		 {
			
			 System.out.println(array [i] + "");
		 }
	
	

	
	
		}
	}

	




im lost my code is not removing the interger even thou i call it how can i get it to remove the interger and update the size of the array. Can someone help me please Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: removing a number and returning the new array

#2 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: removing a number and returning the new array

Posted 20 November 2009 - 06:07 PM

If you want to remove or add to an array, you're better off using arrayList<>. :^:
Was This Post Helpful? 0
  • +
  • -

#3 eserr33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 09

Re: removing a number and returning the new array

Posted 20 November 2009 - 06:10 PM

View Postjimdandy75, on 20 Nov, 2009 - 05:07 PM, said:

If you want to remove or add to an array, you're better off using arrayList<>. :^:

unfortunatly they want us to use there way no list. i wish we could it would be alot easier
Was This Post Helpful? 0
  • +
  • -

#4 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: removing a number and returning the new array

Posted 20 November 2009 - 06:17 PM

I'm probably wrong but I don't think you can use removeAt on an array? It's hard to remove and add elements to an array, that's why there's arrayList. :blink:

This post has been edited by jimdandy75: 20 November 2009 - 06:19 PM

Was This Post Helpful? 0
  • +
  • -

#5 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: removing a number and returning the new array

Posted 20 November 2009 - 06:18 PM

To change the size of an array you really have to redeclare it in a different size.
I suggest making a copy of an array and then redeclare it after that youll have to use a loop to fill newly declared array with the data. Data you would like to remove would be something you look for and then dont add it to new array if found (use if statement for that)
Was This Post Helpful? 0
  • +
  • -

#6 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: removing a number and returning the new array

Posted 20 November 2009 - 06:21 PM

Exactly. And the way I learned, when you get to the point that you must have an array grow or shrink, you then move on to arrayList. Tell your professor he's a dick and use the arrayList :P
Was This Post Helpful? 0
  • +
  • -

#7 eserr33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 09

Re: removing a number and returning the new array

Posted 20 November 2009 - 06:27 PM

View Postjimdandy75, on 20 Nov, 2009 - 05:21 PM, said:

Exactly. And the way I learned, when you get to the point that you must have an array grow or shrink, you then move on to arrayList. Tell your professor he's a dick and use the arrayList :P

thank you ill give it a try
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: removing a number and returning the new array

Posted 20 November 2009 - 07:00 PM

Start by instantiating a new array of the same type as the original, but with its length equal to the originalArray.length-1. Then, copy over the existing elements excluding the first instance of the element to be removed. After you skip over that element, finish copying the rest of the array. Like so:

//Replace E with your datatype, unless you have a firm grasp of generics
public E[] remove(E[] x, E elem){
	E[] temp = new E[x.length-1];
	boolean isSkipped = false;
	for(int i = 0, k = 0; i < temp.length; i++, k++){
		 //element to remove doesn't exist, so return original array
		if(i == temp.length-1 && !isSkipped) return x; 

//Skips over found element
		if(x[k] == elem && !isSkipped){
			 k++;  
			 isSkipped = true;
			 continue;
			 }
		 temp[i] = x[k];
		}
   return temp;
}



@Jimdandy & Eserr: I wouldn't suggest calling your professor a dick. Believe me when I say you will probably fail. Your instructor is trying to get you to understand how ArrayLists work before using them. So relish in the challenge of making something of the same (or even better) quality as what comes in the API.

This post has been edited by macosxnerd101: 20 November 2009 - 07:02 PM

Was This Post Helpful? 0
  • +
  • -

#9 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 311
  • Joined: 30-June 08

Re: removing a number and returning the new array

Posted 20 November 2009 - 07:47 PM

View Postmacosxnerd101, on 20 Nov, 2009 - 06:00 PM, said:

@Jimdandy & Eserr: I wouldn't suggest calling your professor a dick. Believe me when I say you will probably fail. Your instructor is trying to get you to understand how ArrayLists work before using them. So relish in the challenge of making something of the same (or even better) quality as what comes in the API.


I wouldn't really do that, but my point stands. ArrayList is simple to use, it's real simple to add and remove, it grows and shrinks. Array on the other hand is not so easy to work with, that's why you see alot of these threads. I don't see why a professor would want someone to waste their time trying to reinvent the wheel, why not use the time to teach them about how to find what they need in the API. Why teach them the wrong way to do it? :blink:
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,586
  • Joined: 27-December 08

Re: removing a number and returning the new array

Posted 20 November 2009 - 08:07 PM

It is just as important in programming to understand your tools as well as be a creative problem solver. This task definitely allows the programmer to explore the benefits, limitations and capabilities of arrays, and how to solve problems under constraints, something that will happen in the business world. You are right- ArrayLists would be the better choice programatically. However, in terms of expanding one's algorithmic development skills, this assignment is better. Also, it is not the wrong way to do something if it works. It just might not be the best way.

This post has been edited by macosxnerd101: 20 November 2009 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: removing a number and returning the new array

Posted 20 November 2009 - 08:10 PM

View Postjimdandy75, on 20 Nov, 2009 - 06:47 PM, said:

View Postmacosxnerd101, on 20 Nov, 2009 - 06:00 PM, said:

@Jimdandy & Eserr: I wouldn't suggest calling your professor a dick. Believe me when I say you will probably fail. Your instructor is trying to get you to understand how ArrayLists work before using them. So relish in the challenge of making something of the same (or even better) quality as what comes in the API.


I wouldn't really do that, but my point stands. ArrayList is simple to use,

But many teacher want their students to learn the hard way... and it is a good thing

Leaning how to shink, enlarge, clone arrays is essential in the learning process
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,645
  • Joined: 20-September 08

Re: removing a number and returning the new array

Posted 21 November 2009 - 02:14 AM

Quote

E[] temp = new E[x.length-1]; 


Unfortunately the above won't work as it's not possible to create generic arrays like that. You'd need to do:

E[] temp = (E[]) java.lang.reflect.Array.newInstance(elem.getClass(), x.length - 1);



Also, System.arraycopy optimises copying arrays. See

http://www.technojee...t-array-in-java
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1