Im new at Kava, and Im having a trouble to understand what am I doing wrong, and why my code isnt working.
Im trying to implement the bucket sort, using a given array of doubles (double[] ar),
and using arrayList of linkedLists.
here is my code:
int n = ar.length;
//initializing the buckets
ArrayList<LinkedList<Double>> buckets = new ArrayList<LinkedList<Double>>(ar.length);
// Initialize 'n' buckets (each is a LinkedList<Double>) (B[0..n-1])
for (int i = 0; i < n -1; i++) {
buckets.add(i, new LinkedList<Double>());
}
for (int i = 0; i < n; i++) {
buckets.add((int)(n*ar[i]), ar[i]);
}
it
shows an error on "add", and it says:
"The method add(int, LinkedList) in the type ArrayList> is not applicable for the arguments (int, double)"
Thanks.
ArrayList<LinkedList<Double>> buckets[] is your Array of ArrayList OF LinkedList Of DOUBLE.
You are addind a DOUBLE to your ArrayList insted of a LinkedList when doing :
buckets[(int)(n * ar[i])].add((Double)ar[i]);
Consider changing your buckets declaration
Maybe what you wanted is
"ArrayList<Double> buckets[]"
But careful the instanciation changes to
buckets[index] = new ArrayList<Double>();
Related
while adding a integer to object at index of an array, I am getting an error of "arithmetic on pointer to interface id which is not a constant size for this architecture and platform", didn't know how to resolve it.
Please help.
my code is -
if (arrayTotalAmount.count>0) {
int sum = 0;
for (int i = 0; i<=arrayTotalAmount.count; i++) {
sum = (sum+[arrayTotalAmount objectAtIndex:i]);
}
In 4th line I am getting that error.
Thanks
Objective C array only accepts NSObject type. That means it is impossible to insert a primitive value into an NSArray. You are getting an error because objectAtIndex method returns a pointer which points to that NSObject, the arithmetic operations are still valid on pointers but the thing is that the size of a pointer as integer (32bit, 64bit) may differ on device. So one of the solution is typecasting the pointer sum+(int)[arrayTotalAmount objectAtIndex:i] which makes no sense in your case.
The solution you are looking for is probably sum+[[arrayTotalAmount objectAtIndex:i] intValue] or similar. Assuming that array contains NSNumber objects. If the object inside an array is not an NSNumber then your app will fail in runtime showing an error indicating that an object X does not have a method called intValue in which case you will need to figure how to convert object X to your int.
You just need to convert your array object to integer and then add it will work for you.
if (arrayTotalAmount.count>0) {
int sum = 0;
for (int i = 0; i<=arrayTotalAmount.count; i++) {
sum = (sum+[[arrayTotalAmount objectAtIndex:i] intValue]);
}
I have data in my List like below
Click Here
I want to convert that data like this Click Here and also Same project should be in same color.
is it possible to do it through coding? Can anybody help me I have no idea how to get the expected output. I'm using MVC Linq and Entity Framework.
of course you must write custom code
for (int i = 0; i < rows; i++)
{
// Weeks of the month
for (int j = 0; j < 5; j++)
{
// ToDo build another collection
}
}
I have got an array, s[7][7], I read using a ReadFileString() a CSV file into it, the CSV file contained a set of numbers.
I'm looking for numbers greater than 85, stored in the 2-D array, and the column and row the number/element belongs to.
In order to get content of the array just loop it over:
int value = 85;
for(int i=0; i<ArrayRange(s,0); i++){
for(int j=0; j<ArrayRange(s,1); j++){
if (StrToInteger(s[i][j])>value){
// here you have i and j indexes of array
}
}
}
Of course you can have int array[][] instead of string-s for faster work, use StrToInteger() with ReadFileString() for that.
This question already has answers here:
Objective C Equivalent of PHP's "Variable Variables" [duplicate]
(2 answers)
Closed 8 years ago.
For my iOS app, I created a class named Tile which is a subclass of UIImageView.
The tiles are displayed in a kind of an array of 6 rows and 5 column.
I previously created 30 instances of my Tile class. These instances are all named this way: RiCj where i is the row number and j is the column number.
I would like to create a for loop where I would apply a specific treatment to each of my tiles (basically, I want to display the tiles where displayTile is an instance method of the class Tile).
I would love to do something like (I know the code below is incorrect):
for (int i = 1; i <= numberOfRows ; j++) {
for (int j = 1; j <= numberOfColumns ; j++) {
[self.RiCj displayTile];
}
}
I don't know how to do a call to my tiles based on their dynamic string title.
Yes, technically, it is possible - you may use Key-Value Coding like this:
for (int i = 1; i <= numberOfRows; i++) {
for (int j = 1; j <= numberOfColumns; j++) {
NSString* tileName = [NSString stringWithFormat:#"R%dC%d", i, j];
[[self valueForKey:tileName] displayTile];
}
}
But you should not. It won't be a clean solution. Array is a more natural choice here.
Yes, you can actually access a property of a class dynamically by creating a string naming the property then using KVC like so:
NSString *propertyName = [NSString stringWithFormat:#"R%dC%d", i, j];
tile = [self valueForKey:propertyName];
But should you? No, not in this case. It's a horrible hack when the perfectly nice alternative of making an array (or array of arrays) is available.
Here's what array of array creation and access might look like (by using handy Objective C literals for arrays):
NSArray *tiles = #[
#[ tile0C0, tile0C1, tile0C2 ],
#[ tile1C0, tile1C1, tile1C2 ],
#[ tile2C0, tile2C1, tile2C2 ],
];
for (int i = 0; i < numberOfRows ; j++) {
for (int j = 0; j < numberOfColumns ; j++) {
tile = tiles[j][i];
// do stuff with tile
}
}
If I understand correctly, you're trying to access the instances by their variable names dynamically. You can't do that, as your variable name is designed for you, the programmer, and is not available at runtime.
What you can do, however, is to keep a list of your created instances in an array somewhere, and simply iterate over that array when you need to access them.
Alternatively, if you created the 30 tiles as 30 different properties, you could use some dynamic code to get them. At that point, however, I would strongly recommend to use the array technique.
This question already has answers here:
Finding out NSArray/NSMutableArray changes' indices
(3 answers)
Closed 8 years ago.
Here is what i need it to do.
NSArray has 10 objects
NSArray2 has the same 10 objects but in different indexes.
I need to compare if NSArray1 index:5 matches NSArray2 index:5 if not tell me if the object has moved up or down in the NSArray2, same for every other object inside that array.
The objects have the following properties: id and name.
Any suggestion on how i can accomplish this?
If you have RAM to spare you could build a map from the object's id to its index in array 1, then scan array 2 and compare, like:
NSMutableDictionary *map = [[NSMutableDictionary alloc] init];
for (NSUInteger j = 0; j < array1.count; j++) {
id object = array1[j];
map[object.id] = #(j);
}
for (NSUInteger j = 0; j < array2.count; j++) {
id object = array2[j];
id identifier = object.id;
NSUInteger array1Index = [map[identifier] unsignedIntegerValue];
// Compare array1Index to j here.
}
That'll let you compare with a running time that grows like the number of objects in the arrays, but note that you have to spend some extra RAM to make that map. You could compare with only constant RAM costs if you're willing to spend more time:
for (NSUInteger j = 0; j < array1.count; j++) {
id object = array1[j];
NSUInteger k = [array2 indexOfObject:object];
// Compare j and k, note that k could be NSNotFound.
}
And that should have a running time that grows like the product of the array counts.