Arrays Misbehaving

Misbehaving arrays

It turns out the problem was with the firebug debugger. It was not showing the actual array. I'm not sure what it is showing, but I implemented my own object-printing-method (that handles cyclic object references) and it turns out the arrays are correct.

phew thought I was going nuts there for a while.

Arrays misbehaving

The commented line is assigning three of the same reference to the array, so a change to one array will propagate across the other references to it.

As for the 2 arrays vs 3, that's simply a matter of the first line specifying 3 as its first parameter and only specifying 2 array literals in the second line.

To create the nested arrays without having any shared references:

a = Array.new(3) {Array.new(3)}

When passed a block ({...} or do ... end), Array.new will call the block to obtain the value of each element of the array.

Objective-C array of objects misbehaving (EXC_BAD_ACCESS) in TableViewController

In your initWithPrimaryKey:etc: method, you need to retain those values, or use the dot notation to assign them (assuming they are properties declared with the retain keyword). Try this:


- (id)initWithPrimaryKey:(NSInteger)pk categoryId:(NSNumber *)catId carName:(NSString *)n {
if (self = [super init]) {
primaryKey = pk;
categoryId =[catId retain];
name = [n retain];
}

return self;

}

Also, and this is just a style issue, you might consider slightly more verbose names than 'n' and 'pk'. Your future-self will thank you.

array contains() method misbehaving

Use the .contains() method directly on the ArrayList. You can also explicitly return the boolean result back, without the need for return true or return false.

for (LMSEntity lms : registeredLMSs) {
return !lmss.contains(lms.getLmsid());
}

Array formula is misbehaving


=ARRAYFORMULA(VLOOKUP(G3:INDEX(G3:G,COUNT(G3:G)+9),'Program IDs'!A$1:B$17,2,0))

There are 9 blank rows somewhere in your 3000 rows. I've made the necessary changes.

Arrays sort misbehavior for Character array


Strings are immutable (mostly)

Arrays.sort(s5.toCharArray());

The above sorts a copy of the character array and does not modify the actual string value array.

The internal code of toCharArray in JDK 1.8

  char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);

Store and sort the copy(new object in a reference)

char[] copy = s5.toCharArray();
Arrays.sort(copy);

Do not try unless for fun

  Field field = String.class.getDeclaredField("value");
field.setAccessible(true);
String s5 = "peek";
Arrays.sort((char[]) field.get(s5));
System.out.println(s5);

Recursion misbehaving in merge sort

Do,

    for(int j=mid;j<n;j++)
right_arr[j-mid] = arr[j];

And pass the size of whole array

   conquer(left_arr,right_arr,arr,n);

In conquer function,

    int length_left = n/2;
int length_right = n-length_left;

as you cannot find the length of array from its pointer.

Code misbehaving: Trying to search an array for string and print contents of each line

Apart from (unnecessarily) opening the input file multiple times, your logic flaw is here:

if (searchWord.equalsIgnoreCase("all names")) {
for(String temp: list) {
System.out.println(temp);
}
} else if (line.toLowerCase().contains(searchWord.toLowerCase())) {
System.out.println(line);
}

It should be:

for(String line: list) {
if (searchWord.equalsIgnoreCase("all names") || line.toLowerCase().contains(searchWord.toLowerCase()))
System.out.println(temp);
}

Objective-C array of floats misbehaving (EXC_BAD_ACCESS)

You need to alloc your object!

MyObject *k = [[MyObject alloc] init];

if statement is misbehaving for some inputs

The problem is that you aren't sorting lists of numbers, you are sorting lists of strings. If you do a string comparison, '10' comes before '2' in alphabetical order.

All you need to do is define your lists using numbers.

Change

array = ['1', '3', '5', '7', '4', '6', '8', '2', '19', '15', '12', '11', '16', '17', '10', '20'] 

to

array = [1, 3, 5, 7, 4, 6, 8, 2, 19, 15, 12, 11, 16, 17, 10, 20] 


Related Topics



Leave a reply



Submit