Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7612093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:49:25+00:00 2026-05-31T01:49:25+00:00

So I create objects MyFrame, and stores them in a MyFrame array. The problem

  • 0

So I create objects MyFrame, and stores them in a MyFrame array. The problem is when the loop is done, all the objects in the array are equal to the last object created.
Here’s the code:

MyFrame [] response = new MyFrame[8];    
for(int i = 0; i<numberSplit; i++){
   response[i] = new MyFrame("","", infoS[i],"");
}

Here the full class if you run the main you will see that’s in splitInfo and if I manually print each element in the array upon creation it works :

            import java.io.*;
            import java.util.*;

            public class MyFrame {
              // Variable qui donne une valeur fixe aux fanions.
              private static BitSet [] frame = new BitSet [6];
              private static BitSet flag_1 = new BitSet(8);
              private static BitSet address = new BitSet(8);
              private static BitSet control = new BitSet(8);
              private static BitSet fcs = new BitSet(16);
              private static String info = null;

              // Constructeur pour créer des trames
              public MyFrame (BitSet address, BitSet control, String info, BitSet fcs){
                this.frame = frame;
                flag_1.set(1,7);
                this.frame[0] = flag_1;
                this.frame[1] = address;
                this.frame[2] = control;
                this.frame[3] = stringToBitSet(info);
                this.frame[4] = fcs;
                this.frame[5] = flag_1;
              }

              public MyFrame (String address, String control, String info, String fcs){
                this.frame = frame;
                flag_1.set(1,7);
                this.frame[0] = flag_1;
                this.frame[1] = stringToBitSet(address);
                this.frame[2] = stringToBitSet(control);
                this.frame[3] = stringToBitSet(info);
                this.frame[4] = stringToBitSet(fcs);
                this.frame[5] = flag_1;
              }

              // Constructeur qui prend comme entree une trame en String et la longueur du champ
              // d'information
              public MyFrame (String frameS, int infoL){
                int endIL = 24+infoL;
                this.frame = frame;
                this.frame[0] = stringToBitSet(frameS.substring(0,7));
                this.frame[1] = stringToBitSet(frameS.substring(8,16));
                this.frame[2] = stringToBitSet(frameS.substring(16,24));
                this.frame[3] = stringToBitSet(frameS.substring(24,endIL));
                this.frame[4] = stringToBitSet(frameS.substring(endIL,endIL+16));
                this.frame[5] = stringToBitSet(frameS.substring(endIL+16,endIL+24));
              }
              // Constructeur-copieur
              public MyFrame (MyFrame frm){
                  this.frame[0] = (BitSet)frm.frame[0].clone();
                this.frame[1] = (BitSet)frm.frame[1].clone();
                this.frame[2] = (BitSet)frm.frame[2].clone();
                this.frame[3] = (BitSet)frm.frame[3].clone();
                this.frame[4] = (BitSet)frm.frame[4].clone();
                this.frame[5] = (BitSet)frm.frame[5].clone();

              }
              // GETTERS

              public BitSet getAddress(){
                return this.frame[1];
              }

              public BitSet getCtrl(){
                return this.frame[2];
              }

              public BitSet getInfo(){
                return this.frame[3];
              }
                public BitSet getFCS(){
                return this.frame[4];
              }
                  public BitSet getFlag(){
                return this.frame[5];
              }

              public void setAddress(String add){

                this.frame[1] =stringToBitSet(add); 
              }

              // Convertit un BitSet en String.
              public static String bitSetToString(BitSet bs, int length){
                String result ="";
                String tempString="";
                for(int i=0; i < length; i++){
                  boolean temp = bs.get(i);
                  if (temp){
                    tempString = "1";
                  }else{
                    tempString = "0";
                  }
                  result = result.concat(tempString);
                } 
                return result;
              }

              //Convertit un String en BitSet.
              public static BitSet stringToBitSet(String bits){
                int length = bits.length();
                BitSet result = new BitSet (length);
                for(int i=0; i < length; i++){
                  if (bits.charAt(i) == '1'){
                    result.set(i);
                  }
                }
                return result;
              }
              // puisqu'on utilise le PrintWriter on prend la trame et on la transpose en une grande String
              // prend la longueur du champ info en parametre puisqu'il est variable
              public String toString(int infoL){
                String result = "";
                result = result.concat(bitSetToString(this.frame[0],8));
                result = result.concat(bitSetToString(this.frame[1],8));
                result = result.concat(bitSetToString(this.frame[2],8));
                result = result.concat(bitSetToString(this.frame[3],infoL));
                result = result.concat(bitSetToString(this.frame[4],16));
                result = result.concat(bitSetToString(this.frame[5],8));
                return result;
              }

              // print
              public static void print (MyFrame trame, int infoL) {
                System.out.println("Fanion : "+bitSetToString(trame.frame[0],8));
                System.out.println("Addresse : " +bitSetToString(trame.frame[1],8));
                System.out.println("Control : "+bitSetToString(trame.frame[2],8));
                System.out.println("Info : "+bitSetToString(trame.frame[3],infoL));
                System.out.println("FCS : "+bitSetToString(trame.frame[4],16));
                System.out.println("Fanion : "+bitSetToString(trame.frame[5],8));
              }
              // Methode qui prend une trame en parametre et retourne une trame reponse
              public MyFrame [] processFrame(){
                MyFrame [] response = new MyFrame [8];
                BitSet control = this.getCtrl();
                String ctrlS = MyFrame.bitSetToString(control,8);
                BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in)) ;
                String address, info;

                // Determine quel type de trame la trame est
                if(!control.get(0)){
                  System.out.println("Cette trame est de type I");
                }else if (control.get(0) && !control.get(1)){
                  System.out.println("Cette trame est de type S");

                  // RR
                  if (ctrlS.substring(0,4).equals("1000")){
                    System.out.println("RR");

                    // Desire envoyer des trames
                    System.out.println("Desirez-vous envoyer une ou des trame(s)?");
                    for(;;){
                      try{
                        if( userIn.readLine().equals("oui")){
                          System.out.println("Les addresses sont en format binaire et commence toujours par 1");
                          System.out.println("Par exemple : Station Primaire = 10101010");
                          System.out.println("Station Secondaire 00 = 10000000");
                          System.out.println("Station Secondaire 01 = 10000001");
                          System.out.println("Station Secondaire 10 = 10000010");
                          System.out.println("...");
                          System.out.println("Station Secondaire 11111111 = 11111111");
                          System.out.println("\n");
                          System.out.println("Entrer l'addresse pour quel station ces trames sont destinées : ");
                          address = userIn.readLine();

                          System.out.println("Veuillez entrer l'information en binaire que vous-voulez envoyer à l'addresse "+ address +" : ");
                          info = userIn.readLine();

                          response = splitInfo(info);

                          print(response[0],512);

                          // ajoute l'addresse au trames 
                          for(int i = 0; i < response.length-1; i++){
                            response [i].setAddress(address);
                          }

                          // retourne la serie de trames
                          return response;

                          // N'a aucune trame a envoyer
                        }else if (userIn.readLine().equals("non")){
                          response [0] = new MyFrame ("10101010", "10000000","","0000000000000000");
                          return response;

                        }else{
                          System.out.println("Veuillez entrer soit 'oui' soit 'non'");
                        }

                        break;
                      }catch(IOException e){
                        System.out.println(e);
                      }
                    }


                  }
                  // RNR
                  else if(ctrlS.substring(0,1).equals("1001")){
                  }
                  // FRAME ERROR
                  else{}


                }else if (control.get(0) && control.get(1)){
                  System.out.println("Cette trame est de type U : ");
                  // SRNM
                  if (ctrlS.equals("11001001")){
                    System.out.print("SRNM \n");
                    response [0] = new MyFrame("10101010","11001110","","0000000000000000");
                    return response;
                  }
                  // DISC
                  else if(ctrlS.equals("11001010")){
                  }
                  // UA
                  else if(ctrlS.equals("11001110")){        
                    System.out.print("UA \n");
                  }
                  // FRMR : Report receipt of an unacceptable frame
                  else{

                  }

                }

                return null;
              }


              // Methode qui prend un string d'information a envoyer et le subdivise en 
              // string binaire de 512 bits et cree le nombre de trame en consequence
              // par defaut la limite en terme de longeur totale est de 8 (4096 bits)
              public static MyFrame [] splitInfo (String info){
                Integer binVal = new Integer(256);
                String infoBin = "";
                String [] infoS = new String [8];
                MyFrame [] response = new MyFrame [8]; 
                int numberSplit = 0;

                // Transcode les caractere en UTF-8 binaire
                try{
                  byte [] string = info.getBytes("UTF-8");
                  for(int i = 0 ; i<= string.length-1; i++){
                    infoBin = infoBin.concat(binVal.toBinaryString(string[i]));
                  }

                  // Si le message resultant est plus que 64 octets (512 bits)
                  // le splitter en message de 512 bits
                  if (infoBin.length() >511){

                    for (int i = 0; i<=((int)(infoBin.length()/512)); i++){
                      if(((i+1)*512) > infoBin.length()){
                        infoS[i] = infoBin.substring(512*i, infoBin.length());
                      }else{
                        infoS[i] = infoBin.substring(512*i, 512*(i+1));
                      }
                      numberSplit++;

                    }

                    // Creer les trames
                    for (int i = 0; i<numberSplit; i++){
                                MyFrame temp = new MyFrame ("","",infoS[i],"0000000000000000");
                      response[i] = new MyFrame(temp);
                     //print(temp,512);

              }

             print(response[0],512);
              print(response[1],512);
                    //System.out.println(response[0]);
                    //System.out.println(response[1]);

                   //print(response2[0],512);
                    return response;

                  }else{
                    response [0] = new MyFrame("","",infoBin,"0000000000000000");
                    return response;
                  }
                }catch(Exception e){}
                return null;
              }


              public static void main(String args[]){

                MyFrame trame = new MyFrame("0000000"+1,"10001000","","0000000000000011");
                print(trame,0);
                MyFrame trame2 = new MyFrame(trame.toString(0), 0);
                print(trame2,0);
                BitSet compare = new BitSet(8);
                compare.set(0,2,true);
                compare.set(4);
                compare.set(7,true);
                System.out.println("compare : " + compare.toString() + " ctrl : " + trame2.frame[2]);
                System.out.println(trame2.frame[2].equals(compare));
                trame2.setAddress("10111001");
                MyFrame [] x = splitInfo("HEAKdsANSDLASDIASDLMASLDMASKDHAKSNDKAasdasdadasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdsdasdasdasdasdasdasdasdasdasdasdasSMDKASNDKNASKDNASODDFKANSKBFNSKANSDADKABSFKBNASKDNAKSDNOKGJIKASNDIKNASDNA");





              }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:49:27+00:00Added an answer on May 31, 2026 at 1:49 am

    A good way to test if the objects are really different objects is System.out.println(System.identityHashCode(object))

    but the code you posted, as said before, is creating and saving distinct objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create objects from a key/value list. My Problem is, that Object
Noob question here. I'm sure the answer will be create objects, and store them
I have an array of classes and I want to create objects of them.
We normally create objects using the new keyword, like: Object obj = new Object();
I want to create objects dynamically. Right now I'm creating them manually like this
We create Objects in flex by declaring type Object. for example var objSampleObject:Object =
Having a bit of trouble moving objects I've created into an array. So what
I need to create objects and make them available on the web. Similarly to
I know how to use JSON to create objects, but there doesn't seem to
In trying to learn how to create objects in ActionScript, I have had no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.