Deep cloning multidimensional arrays in Java...?

226

Solution 1

You might want to check out the java.util.Arrays.deepEquals and java.util.Arrays.equals methods.

I'm afraid the equals method for array objects performs a shallow comparison, and does not properly (at least for this case) compare the inner Character arrays.

Solution 2

equals() method on arrays is the one declared in Object class. This means that it will only returns true if the object are the same. By the same it means not the same in CONTENT, but the same in MEMORY. Thus equals() on your arrays will never return true as you're duplicating the structure in memory.

Solution 3

A test for equality original.equals(clone); spits out a false. Why? :|

thats because you are creating a new array with new Character[original.length][];.

Arrays.deepEquals(original,clone) should return true.

Share:
226
ZuluDeltaNiner
Author by

ZuluDeltaNiner

Updated on June 25, 2022

Comments

  • ZuluDeltaNiner
    ZuluDeltaNiner almost 2 years

    When I try to dismiss a UIImageView with

    - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
        CGPoint p = [gestureRecognizer locationInView:self.view];
        if (CGRectContainsPoint(_tutorial.frame, p)) {
            _tutorial.hidden = YES;
    
            _transButton.enabled = YES;
            _transButtonEng.enabled = YES;
            _infoButton.enabled = YES;
            _textfield.enabled = YES;
        }
        else {
            NSLog(@"HOW IS THIS EVEN POSSIBLE?!?!?"); //The CGRect is the whole screen
        }
    }
    

    I get this error message:

    MacBook-Pro.local APP_NAME[97086] : CGContextSetFillColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

    It also repeats the same message with CGContextSetFillColorWithColor: replaced by:

    CGContextSetStrokeColorWithColor:
    CGContextSaveGState:
    CGContextSetFlatness:
    CGContextAddPath:
    CGContextDrawPath:
    CGContextRestoreGState:
    CGContextSaveGState:
    CGContextSetFlatness:
    CGContextAddPath:
    CGContextDrawPath:
    CGContextRestoreGState:
    CGContextSetFillColorWithColor:
    CGContextSetStrokeColorWithColor:
    CGContextSetFillColorWithColor:
    CGContextSetStrokeColorWithColor:
    CGContextGetBlendMode:
    CGContextSetBlendMode:
    CGContextFillRects:
    CGContextSetBlendMode:
    CGContextSetFillColorWithColor:
    CGContextSetStrokeColorWithColor:
    CGContextGetBlendMode:
    CGContextSetBlendMode:
    CGContextFillRects:
    CGContextSetBlendMode:
    CGContextSetFillColorWithColor:
    CGContextSetStrokeColorWithColor:
    CGContextGetBlendMode:
    CGContextSetBlendMode:
    CGContextFillRects:
    CGContextSetBlendMode:
    

    I am using the latest version of Xcode 5 and iOS 7.0.3.

    How would I fix these errors?

    • Michael Dautermann
      Michael Dautermann over 10 years
      In the code that you've copied to your question, you should also include the method / function that has the call to "CGContextSetFillColorWithColor".
    • ZuluDeltaNiner
      ZuluDeltaNiner over 10 years
      @MichaelDautermann I don't call any of these anywhere; I use Interface Builder
    • Rob
      Rob over 10 years
      I don't know if it's related, but there are known iOS 7 bugs that cause this sort of behavior: stackoverflow.com/questions/19599266/…
    • ZuluDeltaNiner
      ZuluDeltaNiner over 10 years
      I have seen these, but I was wondering if there was a fix for when UIGestureRecognizer/UIImageView cause the errors
  • ZuluDeltaNiner
    ZuluDeltaNiner over 10 years
    Could you provide an example? I have much trouble reading most types of API Docs.
  • Venkata Raju
    Venkata Raju over 9 years
    This technique works for multidimensional arrays. Just use array in place of this