casting

What's the best way to extract a one-dimensional array from a rectangular array in C#?

旧时模样 提交于 2019-12-19 05:17:18
问题 Say I have a rectangular string array - not a jagged array string[,] strings = new string[8, 3]; What's the best way to extract a one-dimensional array from this (either a single row or a single column)? I can do this with a for loop, of course, but I'm hoping .NET has a more elegant way built in. Bonus points for converting the extracted string array to an object array. 回答1: For a rectangular array: string[,] rectArray = new string[3,3] { {"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"} };

Why does Java ArrayList use per-element casting instead of per-array casting?

泄露秘密 提交于 2019-12-19 05:06:27
问题 What happens inside Java's ArrayList<T> (and probably many other classes) is that there is an internal Object[] array = new Object[n]; , to which T Objects are written. Whenever an element is read from it, a cast return (T) array[i]; is done. So, a cast on every single read. I wonder why this is done. To me, it seems like they're just doing unnecessary casts. Wouldn't it be more logical and also slightly faster to just create a T[] array = (T[]) new Object[n]; and then just return array[i];

CAST(DATETIME AS DATE) over WHERE clause

雨燕双飞 提交于 2019-12-19 04:27:30
问题 I'm using SQL Server 2012 and I would like to know if I write the sentence: SELECT MyDateTimeColumn FROM MyTable WHERE CAST(MyDateTimeColumn AS DATE) = '2014-07-09' is a slower way to trim the time over DATETIME columns, I have searched but I can't find anything about this strict sentence and I don't know how to show that impresive statistics about time consuming cast/convert to probe it myself. 回答1: In SQL 2008+, CAST(foo AS date) is sargable, along with a few other manipulations. Look at

CAST(DATETIME AS DATE) over WHERE clause

回眸只為那壹抹淺笑 提交于 2019-12-19 04:25:41
问题 I'm using SQL Server 2012 and I would like to know if I write the sentence: SELECT MyDateTimeColumn FROM MyTable WHERE CAST(MyDateTimeColumn AS DATE) = '2014-07-09' is a slower way to trim the time over DATETIME columns, I have searched but I can't find anything about this strict sentence and I don't know how to show that impresive statistics about time consuming cast/convert to probe it myself. 回答1: In SQL 2008+, CAST(foo AS date) is sargable, along with a few other manipulations. Look at

Downcasting/Upcasting error at compile time & runtime?

旧街凉风 提交于 2019-12-19 04:02:00
问题 Please check the below program. I have doubt when compiler will issue casting exception at compiler level and when it will be at runtime ? Like in below program, expression I assumed (Redwood) new Tree() should have failed at compiler time as Tree is not Redwood. But it is not failing in compile time , as expected it failed during runtime !!! public class Redwood extends Tree { public static void main(String[] args) { new Redwood().go(); } void go() { go2(new Tree(), new Redwood()); go2(

Programmatically promote QWidget

断了今生、忘了曾经 提交于 2019-12-19 03:43:09
问题 I have an ui file with a QProgressBar in a QWidget . Moreover, I've created my custom progress bar component that inherits from QProgressBar . In QT Designer, I can promote the QProgressBar widget to my custom widget. Is there a way to do this in the widget cpp file instead of using QT Designer? In other words, is there a way to programmatically promote a QWidget into an another custom widget of the same type (a sort of morphing )? Here follows an example: class MyProgressBar : public

what is difference between Convert.ToInt16 and (Int16)

浪子不回头ぞ 提交于 2019-12-19 03:12:25
问题 I had following piece of code try { object s = new object(); s = 10; Console.WriteLine("{0}", Convert.ToInt16(s)); Console.WriteLine("{0}", (Int16)s); } catch (InvalidCastException ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } on the basis of which I have lots of questions.. 1) Is Convert.ToInt16() and (Int16) both are Unboxing operations 2) If both are related to Unboxing then why they are different. Because the above code shows the following error when Console.WriteLine("{0}",

What to pass to the Arrays instance method toArray(T[] a) method? [duplicate]

我是研究僧i 提交于 2019-12-19 02:34:26
问题 This question already has answers here : .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])? (9 answers) Closed last year . If you have an instance of a Collection, say something like: Collection<String> addresses = new ArrayList<String>(); Which were to then be populated with a bunch of values, which is the "best" way, if any, to make use of the toArray() method without requiring a type cast? String[] addressesArray = addresses.toArray(new String[] {}); String[] addressesArray

A conversion that C-style cast can handle, but C++ casts cannot

萝らか妹 提交于 2019-12-18 22:32:26
问题 It is said that C-style cast just tries to apply different combination of C++ casts and the first allowed combination is used. However, I have a feeling that I heard that there are situations that only C-style cast can handle, while none of combination of C++ casts are allowed. Am I wrong? Is that true that any C-style cast in any context (in C++) can be replaced with a proper combination of C++ casts? UPD Thanks to Cheers and hth. - Alf, we have an example that C++ casts cannot handle in the

Read lines containing integers from a file in Python?

♀尐吖头ヾ 提交于 2019-12-18 21:35:11
问题 I have a file format like this: 9 8 1 3 4 1 ... ... Now, I want to get each line as three integers. When I used for line in f.readlines(): print line.split(" ") The script printed this: ['9', '8', '1\r\n'] ['3', '4', '1\r\n'] ... ... How can I get each line as three integers? 回答1: Using the code you have and addressing your specific question of how to convert your list to integers: You can iterate through each line and convert the strings to int with the following example using list