how to find the index no of the arraylist ! urgent pls

Topics merged. Please avoid doublke posting

Page 1 of 1

4 Replies - 12593 Views - Last Post: 23 December 2008 - 11:14 AM Rate Topic: -----

#1 sanni  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-December 08

how to find the index no of the arraylist ! urgent pls

Posted 23 December 2008 - 07:41 AM

sir (martyr2)thank you for your help.

sir hopefully now understand how to declare am arraylist,sir can u tell me how to get the index no of the arraylist and remove item using the index no of the arraylist.i tried to do but i get error pls sir helpme on this please!!!

i didnot do much altering the code

import java.util.ArrayList;

// Class representing the actual mobile entry object
class mobileEntry 
{
	 private int fCredit;
	 private String phoneModel;
	 
	 // Constructor which takes a credit and model
	 public mobileEntry(int freeCredit, String PhoneModelNo) 
	 {
		  fCredit = freeCredit;
		  phoneModel = PhoneModelNo;
	 
	 }
	 
	 // Properties to get credit and phone model
	 public int getCredit() {
		  return fCredit;
	 }
	 
	 public String getPhoneModel() {
		  return phoneModel;
	 }

}


public class MobileShop
{
	 // An arraylist of our mobile entries
	 private ArrayList<mobileEntry> mobile = new ArrayList<mobileEntry>();
   
	 // Create a mobileEntry and then add it to our array list
	public void addmobile(int freecredit,String phonemodelno)
	{
		if(freecredit<0)
		{
			System.out.println("Enter a valid callcredit");
		}
		else
		{
			   // Notice how we make an object and then add it to mobile arraylist
			   mobile.add(new mobileEntry(freecredit,phonemodelno));
			   System.out.println(" Mobile added to display ");
		}

	}
	 
	 // Here we use the credit number to loop through the array list, find the object, then return it.
	 public mobileEntry getEntry(int freeCreditNumber) 
	 {
		  for (mobileEntry entry : mobile) 
		  {
			   if (entry.getCredit() == freeCreditNumber) 
			   {
					return entry;
			   }
		  }
		  
		  
	 }
	 
	 
	 public void showMobilesinStock()
	 {
		 /**
		  * i like to put array index number in front
		  * like this 
		  * "1: w595 with 150 minutes of free calling credit"
		   */
		 for (mobileEntry entry : mobile)
		 {
			 System.out.prinln( phoneModel + "with" + fCredit + " minutes of  free calling credit ");
		}
	
	}
	/**
	 * i want to initialise array index no to some variable calle display no,so user can buy phone using the display no 
	 * if disply no matches the one in the arraylist i want to sell phone and remove it from arraylist	 */
	public void Sellphone(int displayno)
	{
		if(displayno<0)
		{
			System.out.println("Number entered is less than 1");
		}
		else 
		{
			System.out.println("Congragulations you have brought" + phoneModel + "with" fCredit " minutes of free call credit");
			//how to remove pone from arraylist
			//how to find the correct phone using the display no
	
		}   
	
	}
		 
	
	/**
	 * to sell phone using modelno
	 */
	
	public void Sellphone(String PhoneModelNo)
	{
		if(phoneModel
}

/**
 * to display with greatest no of minutes
 */

public void greatestcallcredit()
{

}

}




Is This A Good Question/Topic? 0
  • +

Replies To: how to find the index no of the arraylist ! urgent pls

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: how to find the index no of the arraylist ! urgent pls

Posted 23 December 2008 - 09:55 AM

Hello,

To add an item to a specific spot in an Array list you use the command... NameOfArrayList.Set(); and to remove and item you use... NameOfArrayList.Remove(); Make sure you put the position you want to set or remove, followed by a semi colon and then the item you want to remove. Cheers
Was This Post Helpful? 0
  • +
  • -

#3 sanni  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-December 08

Re: how to find the index no of the arraylist ! urgent pls

Posted 23 December 2008 - 10:45 AM

View Postmarkhazlett9, on 23 Dec, 2008 - 08:55 AM, said:

Hello,

To add an item to a specific spot in an Array list you use the command... NameOfArrayList.Set(); and to remove and item you use... NameOfArrayList.Remove(); Make sure you put the position you want to set or remove, followed by a semi colon and then the item you want to remove. Cheers



sir can you pls show me an example sir,and how to find the index no of the item added to the arraylist.and how to find the item added to the arraylist using the index no of the arraylist
Was This Post Helpful? 0
  • +
  • -

#4 sanni  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-December 08

Re: how to find the index no of the arraylist ! urgent pls

Posted 23 December 2008 - 10:50 AM

guys pls say how to find the indexno for the item added to the arraylist
and how to remove item from the arraylist using the index number
and how to find items added to the arraylist b y using the indexnumber.

code is below pls someone pls me on this pls.
import java.util.ArrayList;

// Class representing the actual mobile entry object
class mobileEntry 
{
	 private int fCredit;
	 private String phoneModel;
	 
	 // Constructor which takes a credit and model
	 public mobileEntry(int freeCredit, String PhoneModelNo) 
	 {
		  fCredit = freeCredit;
		  phoneModel = PhoneModelNo;
	 
	 }
	 
	 // Properties to get credit and phone model
	 public int getCredit() {
		  return fCredit;
	 }
	 
	 public String getPhoneModel() {
		  return phoneModel;
	 }

}


public class MobileShop
{
	 // An arraylist of our mobile entries
	 private ArrayList<mobileEntry> mobile = new ArrayList<mobileEntry>();
   
	 // Create a mobileEntry and then add it to our array list
	public void addmobile(int freecredit,String phonemodelno)
	{
		if(freecredit<0)
		{
			System.out.println("Enter a valid callcredit");
		}
		else
		{
			   // Notice how we make an object and then add it to mobile arraylist
			   mobile.add(new mobileEntry(freecredit,phonemodelno));
			   System.out.println(" Mobile added to display ");
		}

	}
	 
	 // Here we use the credit number to loop through the array list, find the object, then return it.
	 public mobileEntry getEntry(int freeCreditNumber) 
	 {
		  for (mobileEntry entry : mobile) 
		  {
			   if (entry.getCredit() == freeCreditNumber) 
			   {
					return entry;
			   }
		  }
		  
		  
	 }
	 
	 
	 public void showMobilesinStock()
	 {
		 /**
		  * i like to put array index number in front
		  * like this 
		  * "1: w595 with 150 minutes of free calling credit"
		   */
		 for (mobileEntry entry : mobile)
		 {
			 System.out.prinln( phoneModel + "with" + fCredit + " minutes of  free calling credit ");
		}
	
	}
	/**
	 * i want to initialise array index no to some variable calle display no,so user can buy phone using the display no 
	 * if disply no matches the one in the arraylist i want to sell phone and remove it from arraylist	 */
	public void Sellphone(int displayno)
	{
		if(displayno<0)
		{
			System.out.println("Number entered is less than 1");
		}
		else 
		{
			System.out.println("Congragulations you have brought" + phoneModel + "with" fCredit " minutes of free call credit");
			//how to remove pone from arraylist
			//how to find the correct phone using the display no
	
		}   
	
	}
		 
	
	/**
	 * to sell phone using modelno
	 */
	
	public void Sellphone(String PhoneModelNo)
	{
		if(phoneModel
}

/**
* to display with greatest no of minutes
*/

public void greatestcallcredit()
{

}

}


 

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: how to find the index no of the arraylist ! urgent pls

Posted 23 December 2008 - 11:14 AM

you will have to loop using for(int i = 0; i < size(); i++)
rather than for(mobileEntry entry : mobile)
something like:

public deleteEntry(int freeCreditNumber) 
	 {
		  for (int i = 0; i < mobileEntry.size(); i++) 
		  {
			   mobileEntry entry = mobile.get(i); 
			   if (entry.getCredit() == freeCreditNumber) 
			   {
					mobile.remove(i);
					break;
			   }
		  }	 
	 }


Was This Post Helpful? 1

Page 1 of 1