Forum Stats

  • 3,840,120 Users
  • 2,262,569 Discussions
  • 7,901,154 Comments

Discussions

Problem with Array of Object

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Oct 24, 2015 2:15PM in New To Java

Hi,

I have written a program of calendar using array of object. Its storing correct month name in the array of object. But when i store the date & run the program for only one iteration then it runs correctly & shows me the dates for the month of January. But when i run it for 2 iterations (i.e  it should show me the dates for January & February) it shows me the dates for February both the times. Same is true for 3 iterations i.e it shows correct month names but dates are from March month for all 3 iterations. Can some body please guide me?Java Calendar.png

import java.applet.Applet;

import java.awt.*;

import javax.swing.*;

class Calendar{

String monthName;

String[ ] dayName= new String[31];//worst case

int[ ]   dayNo = new int[31];

Calendar(String monthName1, String[ ] dayName1, int[ ] dayNo1){

monthName=monthName1;

dayName= dayName1;

dayNo=dayNo1;

}

void display(int daysInMonth) {

  int i;

String str="";

  String str1=monthName;

  String str2="";

 

for(i=0; i<daysInMonth;++i)

     str2= str2+dayNo[i]+dayName[i]+" ";

str= str1+ str2;

   JOptionPane.showMessageDialog(null, " Inside"+ str);

}

}

public class useCalendar2{

public static void main(String[ ] args) {

String[ ] months= {

                  "January  ",

  "February ",

  "March    ",

  "April    ",

  "May      ",

  "June     ",

  "July     ",

  "August   ",

  "September",

  "October  ",

  "November ",

  "December "

};

int[ ] daysInMonths={31,29,31,30,31,30,31,31,30,31,30,31};

String[] dayName={"Mon", "Tue", "Wed", "Thu","Fri", "Sat", "Sun"};

String[ ] dayNameEntireMonth1= new String[31];

int[ ] dayNo1 = new int[31];

Calendar [ ] obj = new Calendar[13];

int month, day, dayNameIndex;

   dayNameIndex=4;

   String str="";

   for(month =0; month<=2; month++) {

    for(day=1; day<= daysInMonths[month]; ++day) {

        dayNameEntireMonth1[day-1]= dayName[dayNameIndex];

        dayNo1[day-1] = day;

         dayNameIndex++;

         if(dayNameIndex==7)

             dayNameIndex=0;

      

     }

     obj[month] = new Calendar(months[month], dayNameEntireMonth1, dayNo1);

     

   }

   int i;

    for(i=0; i<=2; ++i)

   obj[i].display(daysInMonths[i]);

}

}

I have tried very hard, but i cant find out the error. I have written other programs of arrays of objects, they are running fine but this one has stuck. Some body please guide me.

Zulfi.

Best Answer

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 24, 2015 2:15PM Answer ✓

    Thanks it worked.

    In my previous example, i was not storing values but here i was storing values, so i have to use "new" with non-primitive data type i.e String.

    import java.applet.Applet;

    import java.awt.*;

    import javax.swing.*;

    class Calendar{

    String monthName;

    String[ ] dayName= new String[31];//worst case

    int[ ]   dayNo = new int[31];

    Calendar(String monthName1, String[ ] dayName1, int[ ] dayNo1){

    monthName=monthName1;

    dayName= dayName1;

    dayNo=dayNo1;

    }

    void display(int daysInMonth) {

      int i;

    String str="";

      String str1=monthName;

      String str2="";

     

      for(i=0; i<daysInMonth;++i)

         str2= str2+dayNo[i]+dayName[i]+" ";

    str= str1+ str2;

       JOptionPane.showMessageDialog(null, " Inside"+ str);

    }

    }

    public class useCalendar2{

    public static void main(String[ ] args) {

    String[ ] months= {

                      "January  ",

      "February ",

      "March    ",

      "April    ",

      "May      ",

      "June     ",

      "July     ",

      "August   ",

      "September",

      "October  ",

      "November ",

      "December "

    };

    int[ ] daysInMonths={31,29,31,30,31,30,31,31,30,31,30,31};

    String[] dayName={"Mon", "Tue", "Wed", "Thu","Fri", "Sat", "Sun"};

    String[ ] dayNameEntireMonth1= new String[31];

    int[ ] dayNo1 = new int[31];

    Calendar [ ] obj = new Calendar[13];

    int month, day, dayNameIndex;

       dayNameIndex=4;

       String str="";

       for(month =0; month<=11; month++) {

       dayNameEntireMonth1 = new String[31];

        for(day=1; day<= daysInMonths[month]; ++day) {

            dayNameEntireMonth1[day-1]= dayName[dayNameIndex];

            dayNo1[day-1] = day;

             dayNameIndex++;

             if(dayNameIndex==7)

                 dayNameIndex=0;

          

         }

         obj[month] = new Calendar(months[month], dayNameEntireMonth1, dayNo1);

         

       }

       int i;

        for(i=0; i<=11; ++i)

       obj[i].display(daysInMonths[i]);

    }

    }

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Oct 24, 2015 1:21PM

    java passes arguments "by value"

    In case of an array the "value" is the reference to the array.

    in your initialisation you reuse the array. Therefore all Calendar objects refer to the same single instance of that array.

    This in turn causes that all Calendar objects have the last set of Names created.

    To solve the problem you should create a new array within the outer for loop.

    bye

    TPD

    User_AYF65
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 24, 2015 1:59PM

    Hi,

    Thanks for your reply.

    I actually used the same logic in the following program and it works fine:

    import javax.swing.*;

    class TestArrObject{

    int[ ] marks = new int[ 3];

    String[ ] names= new String[3];

    TestArrObject(int[ ] m1, String[ ] n1) {

    marks= m1;

    names=n1;

    }

    void display( ) {

    String str="";

    int i;

    for(i=0; i<3; ++i){

        str=str+marks[ i ] + " " ;

        str = str +"\n";}

    for(i=0; i<3; ++i){

        str=str+names[ i ] + " ";}

       JOptionPane.showMessageDialog(null,  " " + str);

    }

    }

    public class useTestArrObject{

    public static void main(String[ ] args) {

    int i;

       int[ ][ ] marks={{10, 20, 30},{40, 50, 60},{70, 80, 90}};

       String[][ ] names={{"SSUET1", "SSUE2", "SSUET3"}, {"SSUET4", "SSUE5", "SSUET6"},{"SSUET7", "SSUE8", "SSUET9"}};

       TestArrObject[ ] obj = new TestArrObject[3];

        for(i=0; i<3; ++i)

           obj[i] = new TestArrObject(marks[i], names[i]);

      

        for( i=0; i<3; ++i)

           obj[i].display( );

    }

    }


    <To solve the problem you should create a new array within the outer for loop.>

    Sorry I cant get your point. Which array are you talking about Primitive or non-Primitive. I am already creating an instance of non- primitive data type in the outer loop:

    obj[month] = new Calendar(months[month], dayNameEntireMonth1, dayNo1);

    Please reply me.


    Zulfi.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Oct 24, 2015 2:15PM Answer ✓

    Thanks it worked.

    In my previous example, i was not storing values but here i was storing values, so i have to use "new" with non-primitive data type i.e String.

    import java.applet.Applet;

    import java.awt.*;

    import javax.swing.*;

    class Calendar{

    String monthName;

    String[ ] dayName= new String[31];//worst case

    int[ ]   dayNo = new int[31];

    Calendar(String monthName1, String[ ] dayName1, int[ ] dayNo1){

    monthName=monthName1;

    dayName= dayName1;

    dayNo=dayNo1;

    }

    void display(int daysInMonth) {

      int i;

    String str="";

      String str1=monthName;

      String str2="";

     

      for(i=0; i<daysInMonth;++i)

         str2= str2+dayNo[i]+dayName[i]+" ";

    str= str1+ str2;

       JOptionPane.showMessageDialog(null, " Inside"+ str);

    }

    }

    public class useCalendar2{

    public static void main(String[ ] args) {

    String[ ] months= {

                      "January  ",

      "February ",

      "March    ",

      "April    ",

      "May      ",

      "June     ",

      "July     ",

      "August   ",

      "September",

      "October  ",

      "November ",

      "December "

    };

    int[ ] daysInMonths={31,29,31,30,31,30,31,31,30,31,30,31};

    String[] dayName={"Mon", "Tue", "Wed", "Thu","Fri", "Sat", "Sun"};

    String[ ] dayNameEntireMonth1= new String[31];

    int[ ] dayNo1 = new int[31];

    Calendar [ ] obj = new Calendar[13];

    int month, day, dayNameIndex;

       dayNameIndex=4;

       String str="";

       for(month =0; month<=11; month++) {

       dayNameEntireMonth1 = new String[31];

        for(day=1; day<= daysInMonths[month]; ++day) {

            dayNameEntireMonth1[day-1]= dayName[dayNameIndex];

            dayNo1[day-1] = day;

             dayNameIndex++;

             if(dayNameIndex==7)

                 dayNameIndex=0;

          

         }

         obj[month] = new Calendar(months[month], dayNameEntireMonth1, dayNo1);

         

       }

       int i;

        for(i=0; i<=11; ++i)

       obj[i].display(daysInMonths[i]);

    }

    }

This discussion has been closed.