Problem with pass by reference for array.
Hi,
I am having requirement to process an array of string. which will populate two arrays.
Now, there can or can not be value for two result arrays.
Issue is I am having one main method and I am calling one method to parse the array.
I am passing total three arrays to this method. One is the souceArray and other two arrays will be used to store the results.
Now, if I am setting any one as null in the method, its not getting reflected to the main method.
Below is the sample code,
class MyClass{
publicstaticvoid main(String[] args){
String[] sendTo3 ={"-4738","-4735","-34","-30","-3","-22","-25","-432"};//for -3 + all minus
int[] myList1 =newint[sendTo3.length];
int[] myList2 =newint[sendTo3.length];
String flagVal ="n";
System.out.println(" before call myList1 : "+myList1);
System.out.println(" before call myList1 : "+myList2);
flagVal = parseRecipientList(sendTo3,myList1,myList2);
System.out.println(" after parseRecipientList call myList1 : "+myList1);
System.out.println(" after parseRecipientList call myList1 : "+myList2);
System.out.println(" For -3 + All minus is null1 "+(myList1 ==null));
System.out.println(" For -3 + All minus is null2 "+(myList2 ==null));
printValues(" For -3 + All minus : "+flagVal,sendTo3,myList1,myList2);
System.out.println(" after printValues myList1 : "+myList1);
System.out.println(" after printValues myList1 : "+myList2);
}
privatestaticvoid printValues(String comment,String[] selectedValues,int[] teamIdList,int[] tpIdList){
System.out.println("=========Start :: "+comment+"===========");
System.out.println(" in printValues myList1 : "+teamIdList);
System.out.println(" in printValues myList1 : "+tpIdList);
System.out.println(" Value at first index for selectedValues : "+selectedValues[0]);
if((teamIdList !=null) && teamIdList[0] != 0){
for(int cnt=0;cnt < teamIdList.length;cnt++){
if(teamIdList[cnt] == 0)
break;
System.out.println("Element at teamIdList ["+(cnt+1)+"] "+ teamIdList[cnt]);
}
}else
System.out.println(" teamIdList array is null\t\t ");
System.out.println("\t\t ");
if((tpIdList !=null) && tpIdList[0] != 0){
for(int cnt=0;cnt < tpIdList.length;cnt++){
if(tpIdList[cnt] == 0)
break;
System.out.println("Element at tpIdList ["+(cnt+1)+"] "+ tpIdList[cnt]);
}
}else
System.out.println(" tpIdList array is null\t\t ");
System.out.println("=========End :: "+comment+"===========");
}
privatestatic String parseRecipientList(String[] selectedValues,int[] teamIdList,int[] tpIdList){
String groupFlag ="N";
int toId = 0;
int teamCnt = 0;
int tpIdCnt = 0;
boolean allScoringTeamFlag =false;
System.out.println(" in method tpIdList "+tpIdList );
System.out.println(" in method teamIdList "+teamIdList);
for(int cnt = 0 ;cnt < selectedValues.length;cnt++){
toId = Integer.parseInt(selectedValues[cnt]);
if(toId == 0)
continue;
if(toId == -1){
groupFlag ="N";
selectedValues[0] = selectedValues[cnt];
break;
}elseif(toId == -2){
groupFlag ="N";
allScoringTeamFlag =false;
selectedValues[0] = selectedValues[cnt];
break;
}elseif(toId == -3){
groupFlag ="G";
allScoringTeamFlag =true;
}elseif(!allScoringTeamFlag && toId < -3){
groupFlag ="G";
teamIdList[teamCnt++] = toId;
}
elseif(toId > 0){
groupFlag ="G";
tpIdList[tpIdCnt++]=toId;
}
}//end of for loop
System.out.println(" before allScoringTeamFlag tpIdList "+tpIdList );
System.out.println(" before allScoringTeamFlag teamIdList "+teamIdList);
if(allScoringTeamFlag)
{
System.out.println(" inside allScoringTeamFlag ~~~~ ");
if(tpIdList[0] == 0)
{
System.out.println(" inside allScoringTeamFlag + tpIdLis[0]==0 ~~~~ ");
tpIdList =null;
groupFlag ="N";
selectedValues[0] ="-3";
teamIdList =null;
}else{
System.out.println(" inside else of allScoringTeamFlag + tpIdLis[0]==0 ~~~~ ");
teamIdList[0] = -3;
}
}//end of allScoringTeamFlag
System.out.println(" tpIdList isNull1 "+(tpIdList ==null));
System.out.println(" teamIdList isNull2 "+(teamIdList==null));
System.out.println(" tpIdList "+tpIdList );
System.out.println(" teamIdList "+teamIdList);
return groupFlag;
}//end of parseRecipientList
}

