u**y 发帖数: 202 | 1 Try following code, 1000 equals 1 second.
|
|
c**g 发帖数: 274 | 2 Java array is an object, to create an object, you need to
1) declare
2) initialize (use new to assign a size).
So you can say:
int[] myArray;
myArray = new int[4];
But one of many special things about an array is, you can also
initialize its members, either in an explicitive (such as a loop),
or an implicitvie way. like:
int[] myArray;
myArray = new int[] {1, 2};
you can do this in one line: int[] myArray = new int[] {1, 2};
ALSO, there is a shortcut for the above one line initi |
|
n**********2 发帖数: 648 | 3 【 以下文字转载自 Programming 讨论区 】
发信人: xykkkk (asdf), 信区: Programming
标 题: 老码农冒死揭开行业黑幕:如何编写无法维护的代码(zz)
发信站: BBS 未名空间站 (Fri Nov 28 13:28:27 2014, 美东)
如何编写无法维护的代码
让自己稳拿铁饭碗 ;-)
– Roedy Green(翻译版略有删节)
简介
永远不要(把自己遇到的问题)归因于(他人的)恶意,这恰恰说明了(你自己的)无
能。 — 拿破仑
为了造福大众,在Java编程领域创造就业机会,兄弟我在此传授大师们的秘籍。这些大
师写的代码极其难以维护,后继者就是想对它做最简单的修改都需要花上数年时间。而
且,如果你能对照秘籍潜心修炼,你甚至可以给自己弄个铁饭碗,因为除了你之外,没
人能维护你写的代码。再而且,如果你能练就秘籍中的全部招式,那么连你自己都无法
维护你的代码了!
(伯乐在线配图)
你不想练功过度走火入魔吧。那就不要让你的代码一眼看去就完全无法维护,只要它实
质上是那样就行了。否则,你的代码就有被重写或重构的风险!
总体原则
Quidquid... 阅读全帖 |
|
b********e 发帖数: 58 | 4 If I were you, I may have the following code (assuming it's C):
typedef struct {
int row;
int column;
int* data;
} IArray;
IArray * IArrayInit(int row, int col)
{
IArray *ptr;
ptr = malloc(sizeof(IArray));
ptr->row = row;
ptr->column = col;
ptr->data = malloc(sizeof(int)*row*col);
}
void IArrayFree(IArray *ptr)
{
assert(ptr);
if (ptr->data ) free(ptr->data);
free(ptr);
}
In your main
IArray *myArray;
myArray = IArrayInit(5, 6)
/* do whatever you want with myArray... 阅读全帖 |
|
x****k 发帖数: 2932 | 5 如何编写无法维护的代码
让自己稳拿铁饭碗 ;-)
– Roedy Green(翻译版略有删节)
简介
永远不要(把自己遇到的问题)归因于(他人的)恶意,这恰恰说明了(你自己的)无
能。 — 拿破仑
为了造福大众,在Java编程领域创造就业机会,兄弟我在此传授大师们的秘籍。这些大
师写的代码极其难以维护,后继者就是想对它做最简单的修改都需要花上数年时间。而
且,如果你能对照秘籍潜心修炼,你甚至可以给自己弄个铁饭碗,因为除了你之外,没
人能维护你写的代码。再而且,如果你能练就秘籍中的全部招式,那么连你自己都无法
维护你的代码了!
(伯乐在线配图)
你不想练功过度走火入魔吧。那就不要让你的代码一眼看去就完全无法维护,只要它实
质上是那样就行了。否则,你的代码就有被重写或重构的风险!
总体原则
Quidquid latine dictum sit, altum sonatur.
(随便用拉丁文写点啥都会显得高大上。)
想挫败维护代码的程序员,你必须先明白他的思维方式。他接手了你的庞大程序,没有
时间把它全部读一遍,更别说理解它了。他无非是想快速找到修改代码的位置、改代码
、编译,然后就能交差,... 阅读全帖 |
|
f**********d 发帖数: 4960 | 6 c里面不允许declare动态长度的array.
但因为我的array的长度是从文件中读入,事先并不知道array的长度。
所以我用以下方法:
float *MyArray = malloc(sizeof(float) * N); // N为array的长度的变量
for (i = 0; i < N; ++i)
{
MyArray[i] = Seg[i]; // Seg[N]为array内容的array.
}
但是这样做之后,sizeof(MyArray)为4,也就是一个float的长度。而如果指定N为常数
,比如2。
那么sizeof(MyArray)长度就是8。
两个问题:我这样定义事先不知道长度的array的方法是否正确,如果有问题,那么如
何解决? |
|
d***a 发帖数: 13752 | 7 代码没有问题,可以这样写。你只是要保证分配的空间确实有N个element,否则会有数
组访问溢出。在机器代码层,MyArray[i]的实际意义是*(MyArray+i)。
你不用去管sizeof()的返回值。MyArray是个指针,存放的是内存地址,sizeof(
MyArray)返回的就是指针本身的字节数,在32位机器上就是4个字节。 |
|
o**v 发帖数: 1662 | 8 int[] myArray = new int[2];
myArray[0]=1
myArray[1]=2 |
|
|
w****p 发帖数: 18 | 9 Please see my Java code for this problem:
public class Main {
static int M; static int N;
/* example here. You can replace to your own array */
/* works for arbitrary dimension array */
static int[][] myarray = {{1,2,3}, {4,5,6},{7,8,9},{10,11,12}};
public static void main(String[] args) {
// TODO code application logic here
N=myarray.length;
M=myarray[0].length;
int[] tt=new int[M];
Func(0,tt);
}
static void Func(int k, |
|
a***y 发帖数: 2803 | 10 int** myArray;
myArray = (int**) malloc(nrows * sizeof(int*));
for (int i = 0; i < nrows; i++)
myArray[i] = (int*) malloc(2 * sizeof(int)); |
|
P****y 发帖数: 707 | 11 Why can't I do the following:
int [] myArray;
myArray = {1, 2};
Thanks |
|
m******t 发帖数: 2416 | 12 you are right, looks like if you want to assign it in the declaration
you can do either
int[] myArray = new int[]{1, 2};
or simply
int[] myArray = {1, 2};
But if it's in an assignment, you can't omit "new int[]". |
|
m**c 发帖数: 90 | 13
Only staticaly:
<%
List list = new ArrayList();
...
%>
....
... |
|
b***i 发帖数: 3043 | 14 private class xxx extends yyy {
private static final long serialVersionUID = 31L;
上面是正确的
private static final int[] myArray = {99,-1};却不对,说不能用static
为什么?如果数组没有static, 能保证xxx的不同对象不用分别分配myArray的内存吗? |
|
n*****g 发帖数: 274 | 15 我想定义一个数组的默认大小为常量10,
#define DEFAULT 10;
然后
myArray = new int[DEFAULT];
结果编译的时候总是显示出错,
然后把上一行改为
myArray = new int[10];
编译运行就没有问题了。
可是这样在扩展数组默认大小时就需要更改很多。
怎么样能够定义一个常量呢? |
|
a*******r 发帖数: 7558 | 16 来自主题: Programming版 - IDL一问
有人提出了以下两种办法,供参考。
here are the two methods:
1. Use SCOPE_VARFETCH, which can be compiled in a Runtime program, if
necessary.
The syntax "myArray[i] = scope_varfetch('var')" will copy the value of a
variable named "var" that is
defined in the current scope (scope is where you are running in the current
"call stack" - what routine
call you are currently running in) into 'myArray'.
2. Use EXECUTE, which cannot be compiled in a .sav Runtime program:
EXECUTE treats its string argument as a command th |
|
m***b 发帖数: 11 | 17 想从一个array中随机抽样,比如,取两个.应当是常见问题.
#!/usr/local/bin/perl
#Define an Array and sampling size
my @myArray=('a','c','d','e','f','g','h','j','k','m','n','t');
my $numPick=2;
#use sub:selection_sample()
my @randPick=selection_sample(\@myArray,$numPick);
print (@randPick);
#### subroutine from perlmonk ####
sub selection_sample {
my ($array,$num)=@_;
die "Too few elements (".scalar(@$array).") to select $num from\n"
unless $num<@$array;
my @results;
my $pos=0;
while ( @results |
|
o********7 发帖数: 154 | 18 有一个integer 2D array, column是2列, row的数目不确定, 是从function的参数传
过来的,大小是nrows, 这个array大概的样子就是 myArray[][2]
如何在C中allocate memory呢?我用了下面的编译不过
int (*myArray)[2] = malloc(nrows*sizeof(int[2])); |
|
f**********d 发帖数: 4960 | 19 因为之后我要把MyArray作为一个参数传给一个API的函数,
这个API函数没给当Array的长度不是固定的情况的例子。
我不知道像我那么定义函数是否能知道MyArray的长度。 |
|
a*******r 发帖数: 7558 | 20 来自主题: Computation版 - IDL一问 这里有两种解决办法。感谢Geomatics和James Jones热心回复。
1. Use SCOPE_VARFETCH, which can be compiled in a Runtime program, if
necessary.
The syntax "myArray[i] = scope_varfetch('var')" will copy the value of a
variable named "var" that is
defined in the current scope (scope is where you are running in the current
"call stack" - what routine
call you are currently running in) into 'myArray'.
2. Use EXECUTE, which cannot be compiled in a .sav Runtime program:
EXECUTE treats its string argument as a command that sh |
|
a*******r 发帖数: 7558 | 21 总结一下两种解决办法。感谢Geomatics和James Jones的详尽回复。
1. Use SCOPE_VARFETCH, which can be compiled in a Runtime program, if
necessary.
The syntax "myArray[i] = scope_varfetch('var')" will copy the value of a
variable named "var" that is
defined in the current scope (scope is where you are running in the current
"call stack" - what routine
call you are currently running in) into 'myArray'.
2. Use EXECUTE, which cannot be compiled in a .sav Runtime program:
EXECUTE treats its string argument as a command that |
|
j*****u 发帖数: 1133 | 22 Not sure about Java... does Java has boxing/unboxing also?
if so, primitive (value) type could be in the heap as well:
object o = 1234;
besides boxing, there're other cases that value type does not live in the st
ack, an exmaple is array of value type.
int[] myArray; // all int value of the array lives in the heap.
So, it is not accurate to say "value type goes to the stack, reference type
goes to the heap".
also, there is no variable "name" in the stack, it is actually an address (x
86: 4 bytes... 阅读全帖 |
|
b*******y 发帖数: 1240 | 23 以下程序
copyto和clone都执行shallow copy,但是为什么程序里面反映不出来,谢谢
int[] array = new int[] { 1, 2, 3, 4, 5, 6 };
//create shallow copy by CopyTo
//You have to instantiate your new array first
int[] array2 = new int[myarray.Length];
//but then you can specify how many members of original array yo
u would like to copy
array.CopyTo(array2, 0);
//create shallow copy by Clone
int[] array1;
//here you don't nee... 阅读全帖 |
|
o**v 发帖数: 1662 | 24 or
int[] myArray = {1, 2}; |
|
m******t 发帖数: 2416 | 25 I'm too lazy to test it now, but I think you want to do:
int[] myArray = new int[]{1, 2}; |
|
l*****l 发帖数: 171 | 26 I was using gdb to debug my C/C++ code under linux. I have trouble to display
the elements of a local array. For example:
(gdb) print myArray[0]
This always gives me the address of the first element, not the value.
If the array is dynamically allocated, then there is no problem.
Thanks ahead for your help! |
|
p*u 发帖数: 2454 | 27 try print *myArray
display |
|
v*****x 发帖数: 8 | 28 参见MSDN对C2148的解释:
char MyArray[0x7ffffffff]; // C2148
理由如下:
32位的WINDOWS系统的理论寻址空间是2的32次方, 即4G Bytes.
可惜的是,WINDOWS系统在缺省状态下, 将高位的2G Bytes保留为系统空间. 亦即, 从0
到0x7FFFFFFF是应用程序可以使用的, 而从0x80000000到0xFFFFFFFF是系统使用的.
比如说, 你的应用程序中有一段code:
MyApp::OnUserInput(const char * input) {
if (strcmp(input, "blahblah") == 0) {
MessageBox(...);
}
}
这个MessageBox的Function Body就在高位2GB之中.
如是, Compiler抱错实际上是在你的程序运行之前帮你做了一次安全检查.
事实上, 在一般情况下, 应用程序的实际可用的连续空间是不可能有2GB的. 首先, 你
的应用程序本身需要占用空间, 比如说 100K text, 100K data ... 而且 |
|
l*****e 发帖数: 64 | 29 在c++ template编程中如何判断数据的类型?
比如下面的代码,我需要确定DataT的类型,比如int, float等,
然后根据类型相应修改fprintf的参数输出。
谢了先。
template
void
myArray::write( File &file )
{
for ( unsigned i=Array::_base; !file.error()&&(i<_mysize); i++ )
{
fprintf (file, "%d %g\n", i, float (Array::_data[i]) );
}
} |
|
t****t 发帖数: 6806 | 30 事实上, 原来的写法没有问题, 应该是可以通过的. int[2]是一个type, sizeof(int[2
])也没有问题.
但是如果用C++编译器(或者源文件后缀是.cpp/.C/.cxx之类), 需要作强制转换, 因为
malloc返回的是void*:
int (*myArray)[2] = (int(*)[2])malloc(nrows*sizeof(int[2])); |
|
p*****2 发帖数: 21240 | 31 FP的话是不是要写成Clojure那样的了?
reduce(filter(map(xs, f1)), f2), f3)
或者要extend array,slice
myarray
.map(f1)
.filter(f2)
.reduce(f3) |
|
d****n 发帖数: 12461 | 32 用numpy array不用python list:
import numpy as np
myarray = np.asarray(mylist) |
|