7 Replies - 341 Views - Last Post: 12 October 2012 - 02:50 PM Rate Topic: -----

#1 joseph2324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-April 12

Array List adding/deleting numbers

Posted 12 October 2012 - 01:32 PM

This program have arrayList that contains 5 numbers that user entered [5, 7, 44, 87, 99]. The issue i am having is with addListElement() method that supposed to traverse array to find location where the number should go such as when you add number 9 it should be added to the list like this [5, 7, 9, 44, 87, 99]. What i have done so far when i trying to add the number it adds it to the end of the array and not in specified location.

import java.util.*;

public class Project1P1Client
{

	public static void main(String[] args) 
	{

		Proj1P1IntAryListService p1 = new Proj1P1IntAryListService();
		
		
		p1.createOriginalAryList();
		
		p1.addListElement();


	}//end main

}//end Project1P1Client 





class Proj1P1IntAryListService
{
	
	private ArrayList aryList = new ArrayList();
	
	public void createOriginalAryList()
	{
		
		Scanner scan = new Scanner(System.in);
		boolean valid = false;
		
		int num = -999999999;
		while(!valid)
		{
			//System.out.print("\tEnter an Integer number: ");
			
			
			try 
			{
				
				for(int i = 0; i<5; i++)
				{
					System.out.print("\tEnter an Integer number: ");

					num = scan.nextInt();
					aryList.add(num);
					valid = true;
				
				}//end for
				
				System.out.println("\tOriginal List: " + aryList.toString());
				System.out.println("Current Size of Array List: " + aryList.size());
				aryList.trimToSize();
				System.out.println();
				System.out.println();
				
			}//end try
			
			catch(NumberFormatException fnfe)
			{
				System.out.println("Wrong entry: it is not an Integer! Try again");
			}//end catch
				
		}//end while

	}//end createOriginalAryList()
	

	
	
	
	
	public void addListElement()
	{
		
		System.out.println("Add number to arrayList");
		Scanner scan = new Scanner(System.in);
		int number = scan.nextInt();
	
		int loc = 0;
		for(int i = 0; i<aryList.size(); i++)
		{
			if(loc< aryList.size() && number>aryList.size())
			{
				loc++;
			}
			
		}
		
		
		aryList.add(number);
		System.out.println(aryList.toString());
		
		
	}
	
}//end Proj1P1IntAryListService class



Is This A Good Question/Topic? 0
  • +

Replies To: Array List adding/deleting numbers

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: Array List adding/deleting numbers

Posted 12 October 2012 - 01:53 PM

if(loc< aryList.size() && number>aryList.size())



You are only comparing to the length of the list, and never to its contents.

You should take a few minutes and walk through this code and see what's happening.

        int loc = 0;
        for(int i = 0; i<aryList.size(); i++)
        {
            if(loc< aryList.size() && number>aryList.size())
            {
                loc++;

            }
        }



Take a piece of paper and walk through these steps by hand, and write down the values of i and loc as you go through the loop. See if you notice anything interesting.
Was This Post Helpful? 1
  • +
  • -

#3 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Array List adding/deleting numbers

Posted 12 October 2012 - 01:56 PM

Wouldn't it be easier to just add to the arraylist then sort?

EDIT:What is up with me lately :online2long:

Nevermind. I didn't say anything.

This post has been edited by Kinaces: 12 October 2012 - 01:59 PM

Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: Array List adding/deleting numbers

Posted 12 October 2012 - 01:58 PM

Absolutely it would, but I think the idea is to walk through the traditional approaches before learning the shortcuts.
Was This Post Helpful? 0
  • +
  • -

#5 joseph2324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-April 12

Re: Array List adding/deleting numbers

Posted 12 October 2012 - 02:39 PM

getting closer but still does not work that well
	int loc = 0;
		for(int i =0; i<aryList.size(); i++)
		{
			
			if( number == i && aryList.size() >0)
				{
				     loc++;
				}
			
		}

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: Array List adding/deleting numbers

Posted 12 October 2012 - 02:43 PM

for(int i =0; i<aryList.size(); i++)
  {
  if( number == i && aryList.size() >0)




Both parts of your if condition are problematic.

What are you asking in the first part? Is that really what you want to be asking?


For the second, again, is that really the right question? Will aryList.size() ever change in the loop? Can it ever be negative?
Was This Post Helpful? 1
  • +
  • -

#7 joseph2324  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-April 12

Re: Array List adding/deleting numbers

Posted 12 October 2012 - 02:43 PM

It works if i input numbers closer from the left but not from the right for some reason. Or maybe i only test 1 side of the arraylist?
int loc = 0;
		for(int i =0; i<aryList.size(); i++)
		{
			
			if( number == i && aryList.size() >0)
				{
				     loc++;
				}
			
		}
		
		
		
		
		aryList.add(loc, number);
		
		System.out.println(aryList.toString());

Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,678
  • Joined: 19-March 11

Re: Array List adding/deleting numbers

Posted 12 October 2012 - 02:50 PM

What does this code do? Let number take some value in the range 0..5 and then let it take some value greater than 5.

for (int i = 0; i <5; i++)
{
  if (number == i) 
    System.out.println("Match!");
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1