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
Array List adding/deleting numbers
Page 1 of 17 Replies - 236 Views - Last Post: 12 October 2012 - 02:50 PM
#1
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.
Replies To: Array List adding/deleting numbers
#2
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.
#3
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
Nevermind. I didn't say anything.
EDIT:What is up with me lately
Nevermind. I didn't say anything.
This post has been edited by Kinaces: 12 October 2012 - 01:59 PM
#4
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.
#5
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++;
}
}
#6
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?
#7
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());
#8
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!");
}
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|