You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sudheer <su...@gmail.com> on 2005/01/24 09:59:21 UTC

OT newbie - java class question

Hi friends,

I have a  method like this, the 3rd param, category contains
ArrayList of classes Category.

Here I am copying the  Category  class from the ArrayList to
lALCategories (Line 11) and storing its property name into a
local variable lsName (Line15) and doing some processing and
writing it back on line 19.

Then adding this class to lALOutPut. Here the changes I made
to the class property name is  reflected in HashMap category
also. I don't want the changes to be reflected in the passed
HashMap category. How do I do this? Please help.

TIA

Sudheer

01. public static ArrayList listTree(Integer parentID, HashMap tree, HashMap
category)
02. {
03.    Category lCCategory = null;
04.    ArrayList lALCategories = new ArrayList(), lALOutPut = new
ArrayList();
05.    Integer liNext = null;
06.    Integer liTree[] = (Integer[]) tree.get(parentID);
07.    String lsName = "";
08.    for (short liElm = 0; liElm < liTree.length; liElm++)
09.    {
10.      liNext = liTree[liElm];
11.      lALCategories = (ArrayList) category.get(liNext);
12.      for (int liIElm = 0; liIElm < lALCategories.size(); liIElm++)
13.      {
14.        lCCategory = (Category) lALCategories.get(liIElm);
15.    lsName = lCCategory.getName()
16.    /*
17.    Some processing here on lsName.
18.    */
19.        lCCategory.setName(lsName);
20.        lALOutPut.add(lCCategory);
21.      }
22.    }
23.    return lALOutPut;
24.}

RE: OT newbie - Thanks eveybody

Posted by Sudheer <su...@gmail.com>.
Hi,

  Thank u very much for all the support and your valuable advise.

Sudheer

-----Original Message-----
From: Dakota Jack [mailto:dakota.jack@gmail.com]
Sent: Monday, January 24, 2005 9:21 PM
To: Struts Users Mailing List
Subject: Re: OT newbie - java class question


The answer is to clone the Category object prior to setting the name
and add that clone to the IALOutpPut object.  I know you have not
asked, but I think you might want to make things a bit more readable
for others by doing at least something *like* as follows.  Your
present code is very hard to read, which is an important part of good
code.  I assume you are fairly new at this game and hope you take this
as an attempt to be helpful.  If you are a grizzlied veteran with your
own ideas, forget what I said.

/* This method takes all the Category held in ArrayLists
   objects from the HashMap category  (with a key of
   liTree.length or greater) and puts them in one master
   ArrayList (1ALOutPut) after processing their names. */

public ArrayList getCategoriesTree(Integer parentId, HashMap tree,
HashMap category) {
  return listTree(getLiTree(parentId,tree),category);
}


/* This method gets an array of Integers held in the HashMap
   tree under the key parentId. */

private Integer [] getLiTree(Integer parentId, HashMap tree) {
  return (Integer[])tree.get(parentId);
}

private static ArrayList listTree(Integer []   liTree,
                                             HashMap category) {

  /* The map category holds ArrayLists (which contain
     Category objects) which are temporarily processed
     under this alias.  The keys are integers relating
     to the length of liTree. */

  ArrayList lALCategories = null;

  /* This ArrayList holds all the Category objects in
     all the ArrayLists in category where the key is
     liTree.length or greater.  Prior to putting the
     Category objects in this extended ArrayList, the
     names in the Category objects are processed. */

  ArrayList lALOutPut     = null;

  Integer      liNext         = null;
  Category  lCCategory  = null;
  String      lsName       = null;
  int           treeLength   = liTree.length;
  int           arrayLength = 0;

	for (int i = 0; i < treeLength; i++) {
		liNextKey     = liTree[i];
		lALCategories = (ArrayList)category.get(liNextKey);
                arrayLength   = 1ALCategories.size();


		for (int j = 0; j < arrayLength; j++) {
			lCCategory = (Category)lALCategories.get(j);
			lsName = lCCategory.getName()
			/*
			Some processing here on lsName.
			*/
			lCCategory.setName(lsName);
			lALOutPut.add(lCCategory);
		}
	}
	return lALOutPut;
}

Jack


On Mon, 24 Jan 2005 14:29:21 +0530, Sudheer <su...@gmail.com> wrote:
> Hi friends,
>
> I have a  method like this, the 3rd param, category contains
> ArrayList of classes Category.
>
> Here I am copying the  Category  class from the ArrayList to
> lALCategories (Line 11) and storing its property name into a
> local variable lsName (Line15) and doing some processing and
> writing it back on line 19.
>
> Then adding this class to lALOutPut. Here the changes I made
> to the class property name is  reflected in HashMap category
> also. I don't want the changes to be reflected in the passed
> HashMap category. How do I do this? Please help.
>
> TIA
>
> Sudheer
>
> 01. public static ArrayList listTree(Integer parentID, HashMap tree,
HashMap
> category)
> 02. {
> 03.    Category lCCategory = null;
> 04.    ArrayList lALCategories = new ArrayList(), lALOutPut = new
> ArrayList();
> 05.    Integer liNext = null;
> 06.    Integer liTree[] = (Integer[]) tree.get(parentID);
> 07.    String lsName = "";
> 08.    for (short liElm = 0; liElm < liTree.length; liElm++)
> 09.    {
> 10.      liNext = liTree[liElm];
> 11.      lALCategories = (ArrayList) category.get(liNext);
> 12.      for (int liIElm = 0; liIElm < lALCategories.size(); liIElm++)
> 13.      {
> 14.        lCCategory = (Category) lALCategories.get(liIElm);
> 15.    lsName = lCCategory.getName()
> 16.    /*
> 17.    Some processing here on lsName.
> 18.    */
> 19.        lCCategory.setName(lsName);
> 20.        lALOutPut.add(lCCategory);
> 21.      }
> 22.    }
> 23.    return lALOutPut;
> 24.}
>
>


--
------------------------------

"You can lead a horse to water but you cannot make it float on its back."

~Dakota Jack~

"You can't wake a person who is pretending to be asleep."

~Native Proverb~

"Each man is good in His sight. It is not necessary for eagles to be crows."

~Hunkesni (Sitting Bull), Hunkpapa Sioux~

-----------------------------------------------

"This message may contain confidential and/or privileged information.
If you are not the addressee or authorized to receive this for the
addressee, you must not use, copy, disclose, or take any action based
on this message or any information herein. If you have received this
message in error, please advise the sender immediately by reply e-mail
and delete this message. Thank you for your cooperation."

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: OT newbie - java class question

Posted by Dakota Jack <da...@gmail.com>.
The answer is to clone the Category object prior to setting the name
and add that clone to the IALOutpPut object.  I know you have not
asked, but I think you might want to make things a bit more readable
for others by doing at least something *like* as follows.  Your
present code is very hard to read, which is an important part of good
code.  I assume you are fairly new at this game and hope you take this
as an attempt to be helpful.  If you are a grizzlied veteran with your
own ideas, forget what I said.

/* This method takes all the Category held in ArrayLists 
   objects from the HashMap category  (with a key of 
   liTree.length or greater) and puts them in one master 
   ArrayList (1ALOutPut) after processing their names. */

public ArrayList getCategoriesTree(Integer parentId, HashMap tree,
HashMap category) {
  return listTree(getLiTree(parentId,tree),category);
}


/* This method gets an array of Integers held in the HashMap
   tree under the key parentId. */

private Integer [] getLiTree(Integer parentId, HashMap tree) {
  return (Integer[])tree.get(parentId);
}

private static ArrayList listTree(Integer []   liTree, 
                                             HashMap category) {

  /* The map category holds ArrayLists (which contain
     Category objects) which are temporarily processed 
     under this alias.  The keys are integers relating
     to the length of liTree. */

  ArrayList lALCategories = null;  

  /* This ArrayList holds all the Category objects in
     all the ArrayLists in category where the key is
     liTree.length or greater.  Prior to putting the
     Category objects in this extended ArrayList, the
     names in the Category objects are processed. */

  ArrayList lALOutPut     = null; 
  
  Integer      liNext         = null;
  Category  lCCategory  = null;
  String      lsName       = null;
  int           treeLength   = liTree.length;
  int           arrayLength = 0;
  
	for (int i = 0; i < treeLength; i++) {
		liNextKey     = liTree[i];
		lALCategories = (ArrayList)category.get(liNextKey);
                arrayLength   = 1ALCategories.size();
    
		
		for (int j = 0; j < arrayLength; j++) {
			lCCategory = (Category)lALCategories.get(j);
			lsName = lCCategory.getName()
			/*
			Some processing here on lsName.
			*/
			lCCategory.setName(lsName);
			lALOutPut.add(lCCategory);
		}
	}
	return lALOutPut;
}

Jack


On Mon, 24 Jan 2005 14:29:21 +0530, Sudheer <su...@gmail.com> wrote:
> Hi friends,
> 
> I have a  method like this, the 3rd param, category contains
> ArrayList of classes Category.
> 
> Here I am copying the  Category  class from the ArrayList to
> lALCategories (Line 11) and storing its property name into a
> local variable lsName (Line15) and doing some processing and
> writing it back on line 19.
> 
> Then adding this class to lALOutPut. Here the changes I made
> to the class property name is  reflected in HashMap category
> also. I don't want the changes to be reflected in the passed
> HashMap category. How do I do this? Please help.
> 
> TIA
> 
> Sudheer
> 
> 01. public static ArrayList listTree(Integer parentID, HashMap tree, HashMap
> category)
> 02. {
> 03.    Category lCCategory = null;
> 04.    ArrayList lALCategories = new ArrayList(), lALOutPut = new
> ArrayList();
> 05.    Integer liNext = null;
> 06.    Integer liTree[] = (Integer[]) tree.get(parentID);
> 07.    String lsName = "";
> 08.    for (short liElm = 0; liElm < liTree.length; liElm++)
> 09.    {
> 10.      liNext = liTree[liElm];
> 11.      lALCategories = (ArrayList) category.get(liNext);
> 12.      for (int liIElm = 0; liIElm < lALCategories.size(); liIElm++)
> 13.      {
> 14.        lCCategory = (Category) lALCategories.get(liIElm);
> 15.    lsName = lCCategory.getName()
> 16.    /*
> 17.    Some processing here on lsName.
> 18.    */
> 19.        lCCategory.setName(lsName);
> 20.        lALOutPut.add(lCCategory);
> 21.      }
> 22.    }
> 23.    return lALOutPut;
> 24.}
> 
> 


-- 
------------------------------

"You can lead a horse to water but you cannot make it float on its back."

~Dakota Jack~

"You can't wake a person who is pretending to be asleep."

~Native Proverb~

"Each man is good in His sight. It is not necessary for eagles to be crows."

~Hunkesni (Sitting Bull), Hunkpapa Sioux~

-----------------------------------------------

"This message may contain confidential and/or privileged information.
If you are not the addressee or authorized to receive this for the
addressee, you must not use, copy, disclose, or take any action based
on this message or any information herein. If you have received this
message in error, please advise the sender immediately by reply e-mail
and delete this message. Thank you for your cooperation."

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: OT newbie - java class question

Posted by Jim Barrows <ji...@gmail.com>.
On Mon, 24 Jan 2005 14:29:21 +0530, Sudheer <su...@gmail.com> wrote:
> Hi friends,
> 
> I have a  method like this, the 3rd param, category contains
> ArrayList of classes Category.
> 
> Here I am copying the  Category  class from the ArrayList to
> lALCategories (Line 11) and storing its property name into a
> local variable lsName (Line15) and doing some processing and
> writing it back on line 19.
> 
> Then adding this class to lALOutPut. Here the changes I made
> to the class property name is  reflected in HashMap category
> also. I don't want the changes to be reflected in the passed
> HashMap category. How do I do this? Please help.

This is pretty basic Java.  Java is pass by reference, so there is
only one copy of the ICCategory.  If you want multiple copies your
going to have to clone it.

> 
> TIA
> 
> Sudheer
> 
> 01. public static ArrayList listTree(Integer parentID, HashMap tree, HashMap
> category)
> 02. {
> 03.    Category lCCategory = null;
> 04.    ArrayList lALCategories = new ArrayList(), lALOutPut = new
> ArrayList();
> 05.    Integer liNext = null;
> 06.    Integer liTree[] = (Integer[]) tree.get(parentID);
> 07.    String lsName = "";
> 08.    for (short liElm = 0; liElm < liTree.length; liElm++)
> 09.    {
> 10.      liNext = liTree[liElm];
> 11.      lALCategories = (ArrayList) category.get(liNext);
> 12.      for (int liIElm = 0; liIElm < lALCategories.size(); liIElm++)
> 13.      {
> 14.        lCCategory = (Category) lALCategories.get(liIElm);
> 15.    lsName = lCCategory.getName()
> 16.    /*
> 17.    Some processing here on lsName.
> 18.    */
> 19.        lCCategory.setName(lsName);
> 20.        lALOutPut.add(lCCategory);
> 21.      }
> 22.    }
> 23.    return lALOutPut;
> 24.}
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


R: OT newbie - java class question

Posted by Amleto Di Salle <di...@di.univaq.it>.
Hi,
You can clone the Category object implementanting the
java.lang.Cloneable interface (see the java api documentation) and
correct the code in the following way:

> 12.      for (Iterator iterator = lALCategories.iterator();
iterator.hasNext(); )
> 13.      {
> 14.        lCCategory = ((Category) iterator.next()).clone();
> 15.    lsName = lCCategory.getName()
> 16.    /*
> 17.    Some processing here on lsName.
> 18.    */
> 19.        lCCategory.setName(lsName);
> 20.        lALOutPut.add(lCCategory);
> 21.      }

BR
/Amleto

> -----Messaggio originale-----
> Da: Sudheer [mailto:sudheer.clt@gmail.com] 
> Inviato: lunedì 24 gennaio 2005 9.59
> A: Struts Users Mailing List
> Oggetto: OT newbie - java class question
> 
> 
> Hi friends,
> 
> I have a  method like this, the 3rd param, category contains 
> ArrayList of classes Category.
> 
> Here I am copying the  Category  class from the ArrayList to 
> lALCategories (Line 11) and storing its property name into a 
> local variable lsName (Line15) and doing some processing and 
> writing it back on line 19.
> 
> Then adding this class to lALOutPut. Here the changes I made
> to the class property name is  reflected in HashMap category 
> also. I don't want the changes to be reflected in the passed 
> HashMap category. How do I do this? Please help.
> 
> TIA
> 
> Sudheer
> 
> 01. public static ArrayList listTree(Integer parentID, 
> HashMap tree, HashMap
> category)
> 02. {
> 03.    Category lCCategory = null;
> 04.    ArrayList lALCategories = new ArrayList(), lALOutPut = new
> ArrayList();
> 05.    Integer liNext = null;
> 06.    Integer liTree[] = (Integer[]) tree.get(parentID);
> 07.    String lsName = "";
> 08.    for (short liElm = 0; liElm < liTree.length; liElm++)
> 09.    {
> 10.      liNext = liTree[liElm];
> 11.      lALCategories = (ArrayList) category.get(liNext);
> 12.      for (int liIElm = 0; liIElm < lALCategories.size(); liIElm++)
> 13.      {
> 14.        lCCategory = (Category) lALCategories.get(liIElm);
> 15.    lsName = lCCategory.getName()
> 16.    /*
> 17.    Some processing here on lsName.
> 18.    */
> 19.        lCCategory.setName(lsName);
> 20.        lALOutPut.add(lCCategory);
> 21.      }
> 22.    }
> 23.    return lALOutPut;
> 24.}
> 
> -- 
> No virus found in this incoming message.
> Checked by AVG Anti-Virus.
> Version: 7.0.300 / Virus Database: 265.7.2 - Release Date: 21/01/2005
>  
> 

-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.7.2 - Release Date: 21/01/2005
 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org