Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: the values of my array are totally modified by an affectation

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default the values of my array are totally modified by an affectation

    Hello,
    I am new in java programming. I'm doing an exercise on a Mooc and I have some trouble with my code.
    The following code is supposed to return an array and I will print the values that are contained in it.
    public static Element[] LZ77(int[] tab, int tailleFenetre)
    	{
     
    		int taille = LZ77Longueur(tab,tailleFenetre);
    		Element[] t = new Element [taille];
    		Occurrence test = new Occurrence(0,0);
    		Element temp = new Element(test,0);
    		int c = 0;
    		int element = 0;
    		int i = 0;
    		int next = 0;
    		while (c < tab.length && tab[i+1]!=2)
    		{
    			 test = plusLongueOccurrence(tab,c,tailleFenetre);
    			 int k = test.taille;
    			 int r = test.retour;
    			 next = tab[c+k];
    			 c = c + k + 1;
    			 test.retour = r;
    			 test.taille = k;
    			 //temp.s = next;
    			 temp.e = test;
    			 temp.s = next;
    			 t[element]=temp;
    			 element ++;
     
    	   }
    		t[t.length-1].s = 2;
     
    		return t;
    	}
    My problem is that the values of the array t are modified by the current affectation. How could I do to solve that?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: the values of my array are totally modified by an affectation

    the values of the array t are modified by the current affectation
    Can you post some program output that shows what you are having problems with? Add some comments that say what the output should look like and give an example.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: the values of my array are totally modified by an affectation

    Well, that's the output of my program
    (10,4)2(10,4)2(10,4)2(10,4)2(10,4)2(10,4)2(10,4)2
    Normally, I should obtain this:
    (0,0)0(0,0)1(2,2)1(5,3)0(4,4)0(10,1)1(10,4)2

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: the values of my array are totally modified by an affectation

    How can the program be compiled and executed for testing?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: the values of my array are totally modified by an affectation

    Can I provide you all my code? For testing , there is two function that should be called.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: the values of my array are totally modified by an affectation

    Not all the code. Just enough to compile, execute and show the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: the values of my array are totally modified by an affectation

    public class LZ77
    {
    	public static Occurrence plusLongueOccurrence(
    		      int[] t,
    		      int positionCourante,
    		      int tailleFenetre
    		    )
     
     
    	 {
    		 int c = positionCourante;
    		 int f;
    		 int count = 0;
    		 int occurrence = 0;
    		 Occurrence test = new Occurrence(0,0);
    		 int back = 0;
    		 String str1 = "";
    		 String str2 = "";
    		 int i = 0;
    		 if (c-tailleFenetre > 0)
    		 {
    			 f = c-tailleFenetre;
    		 }
    		 else
    		 {
    			 f = 0;
    		 }
    		 //String str = Integer.toString(i);
    		 if (t.length == 0)
    		 {
     
    			 return test;	
    		 }
    		 else{
     
     
    			 while( f+ i < c && c + i < t.length )
    			 {
     
    				 	str1 = str1 + Integer.toString(t[f + i]);
    				 	str2 = str2 + Integer.toString(t[c+i]);
     
    					 if (str1.equals(str2))
    					 {
     
    					 	count ++;
    					 	//System.out.println(str1 +" "+str2 + " count "+ count);
    						if (occurrence < count ) //&& f + i <=tailleFenetre
    						{
    //						 System.out.print(" i " + i);
    //						 System.out.print(" f " + f + " ");
    						 occurrence = count;
    						 back = c-f;
    						 i++;
    						}
    						else
    						{
    							 i++;
     
    						}
    					}
    					 else
    					 {
    					 i = 0;
    					 f++;
    					 str1 = "";
    					 str2="";
    					 count = 0;
     
    					 }
    			 	}
     
    			 }
    		 test.taille = occurrence;
    		 test.retour = back;
    		 return test;}
     
    	public static int LZ77Longueur(int[] t, int tailleFenetre)
    	  {
    		int c = 0;
    		int element = 0;
    		int i = 0;
    		Occurrence test1 = new Occurrence(0,0);
    		while (c < t.length && t[i+1]!=2)
    		{
    			 test1 = plusLongueOccurrence(t,c,tailleFenetre);
    			 int k = test1.taille;
    			 c = c + k + 1;
    			 element ++;
     
    	   }
    		return element;
     
    	}
     
     
    	/**
    	 * @param tab
    	 * @param tailleFenetre
    	 * @return
    	 */
    // the part of the code that doesn't work
    	[COLOR="#FF0000"]public static Element[] LZ77(int[] tab, int tailleFenetre)
    	{
     
    		int taille = LZ77Longueur(tab,tailleFenetre);
    		Element[] t = new Element [taille];
    		Occurrence test = new Occurrence(0,0);
    		Element temp = new Element(test,0);
    		int c = 0;
    		int element = 0;
    		int i = 0;
    		int next = 0;
    		while (c < tab.length && tab[i+1]!=2)
    		{
    			 test = plusLongueOccurrence(tab,c,tailleFenetre);
    			 int k = test.taille;
    			 int r = test.retour;
    			 next = tab[c+k];
    			 c = c + k + 1;
    			// t[element].e.retour = r;
    			 test.retour = r;
    			 test.taille = k;
    			 temp.s = next;
    			 temp.e = test;
    			 temp.s = next;
    			 t[element]=temp;
    			 element ++;
     
    	   }
    		t[t.length-1].s = 2;
     
    		return t;
    	}[/COLOR]
     
     
    	public static void afficheEncode(Element[] tab)
    	{
    		int r = 0;
    		int t = 0;
    		int next = 0;
    		for (int i = 0; i<tab.length; i++)
    		{
    			r = tab[i].e.retour;
    			t = tab[i].e.taille;
    			next = tab[i].s;
    			System.out.print("("+ r + "," + t + ")" + next);
    		}
    		System.out.print("\n");
    	}
     
     
     
    }
     class Occurrence 
     {
    	 public int taille;
    	 public int retour;
     
    	 Occurrence ( int retour, int taille) 
    	 {
     
    		 this.taille = taille;
    		 this.retour = retour;
     
    	 }
     }	 
     
     
    	 class Element
    	{
    		public Occurrence e;
    		public int s;
    		  Element (Occurrence e, int s) 
    		  {
    			  this.e = e;
    			  this.s = s;
    		  }
     
    	}

    For testing I use the following code:
    final static int[] testsLz = { 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 2 };
    for (int[] line: Data.testsLz)
        	LZ77.afficheEncode(LZ77.LZ77(line, 10));
              System.out.printf("done\n");
    It's only the function LZ77 that doesn't work.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: the values of my array are totally modified by an affectation

    There is no main() method in the code so it doesn't execute.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: the values of my array are totally modified by an affectation

    public class Test {
     
      public static void main(String[] args) {
    testLz3();
    public static void testLz3() {
     
        final static int[]line =  { 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 2 }
        System.out.printf("testLz3 ");
        for (int[] line: Data.testsLz)
        	LZ77.afficheEncode(LZ77.LZ77(line, 10));
          //LZ77.afficheEncode(LZ77.LZ77(line, Data.tailleFenetre));
        System.out.printf("done\n");
     
      }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: the values of my array are totally modified by an affectation

    There are many compiler errors in that code. Please fix them.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. how to find 2nd largest array if array values like{10,20,92,81,92,34}
    By anjijava16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2013, 05:59 PM
  2. Replies: 2
    Last Post: October 26th, 2013, 10:56 AM
  3. basic recursion problem - code needs to be modified
    By ash12 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 6th, 2012, 09:20 AM
  4. Updation of webpage to the last modified date
    By Revathy G in forum Java Programming Tutorials
    Replies: 1
    Last Post: May 6th, 2009, 05:47 AM
  5. Updation of webpage to the last modified date
    By Revathy G in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 6th, 2009, 05:47 AM