我可以在C#中获得一些与Java数组等效的帮助吗? [英] Can I get some help with a java array equivalent in C#?

查看:98
本文介绍了我可以在C#中获得一些与Java数组等效的帮助吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有一个程序正在填充List< float> c_dat和t_dat一次带有 2,4或6 个数字。然后下一步做一些数学计算方差并通过追加它们来跟踪事物。问题是qC [0]项经常是0,这导致我在代码中尝试进行更多计算时会遇到很多问题。我不是python专家 - 我可以获得以下代码片段的一些指导吗?



Hi there,
I have a program that is filling List<float> c_dat and t_dat with 2, 4, or 6 numbers at a time. Then the next step does some math to figure out variance and keep track of things by appending them. The problem is that the qC[0] item is quite often 0 and this is causing me a lot of issues later on in the code trying to do more calculations. I'm no python expert - can I get some guidance on the following code snippet?

if C_count >= 0 and T_count >= 0:
	
	qC=[0.0,0.0]
	qT=[0.0,0.0]
						
	for j in range(len(C_dat)/2):
		#print dip_dat[2*j],dip_dat[2*j+1]
		m = C_dat[2*j]+C_dat[2*j+1]
		qC[0]+= float(C_dat[2*j])	# ref allele freq						
		qC[1]+= float(m)
	for j in range(len(T_dat)/2):
		m = T_dat[2*j]+T_dat[2*j+1]
		qT[0]+= float(T_dat[2*j])	# ref allele freq						
		qT[1]+= float(m)

	#print qT[1],qC[1]						
	if (qT[1] >= Min_reads and qC[1] >= Min_reads):
# Here I am outputting a standardized data format
# columns: scaffold, position, ref allele count 1, total count 1,ref allele count 2, total count 2
		outkk.write(cols[0]+"\t"+cols[1]+"\t"+str(qC[0])+"\t"+str(qC[1])+"\t"+str(qT[0])+"\t"+str(qT[1])+"\n")
		outcomes[2]+=1#print "here"
		Locations.append(cols[0]+"_"+cols[1])
		num_snps+=1
		qC_hat=qC[0]/qC[1]
		qT_hat=qT[0]/qT[1]

		var_C = 1.0/qC[1]
		var_T = 1.0/qT[1]





我尝试过:



我试过制作只能附加到qC和qC_m的double和float变量,我尝试过arraylists和list但结果是一样的。



这是我目前的版本,但它不起作用out out ...



What I have tried:

I've tried making double and float variables that just get appended to such as qC and qC_m and I've tried arraylists and lists but the results are the same.

Here's my current version but it's not working out...

if (C_count >= 0 && T_count >= 0)
    {

        double[] qC = { 0.0, 0.0 };
        double[] qT = { 0.0, 0.0 };


        var range1 = Enumerable.Range(0, (C_dat.Count / 2));
        foreach (int i in range1)
        {
            double m = Convert.ToDouble(C_dat[2 * i]) + Convert.ToDouble(C_dat[2 * i + 1]);
            //qC[0] is fairly consistently 0
            qC[0] = qC[0] + Convert.ToDouble(C_dat[2 * i]);
            if (C_dat.Count == 2 && qC[0] == 0)
            {
                Console.WriteLine(" C_dat count = " + C_dat.Count + " referencing C_dat[2*i] or C_dat " + 2 * i + " item = " + C_dat[2 * i]);
            }
            qC[1] = qC[1] + m;

        }
        var range2 = Enumerable.Range(0,(T_dat.Count/2));
        foreach (int i in range2)
        {
            double m = Convert.ToDouble(T_dat[2 * i]) + Convert.ToDouble(T_dat[2 * i + 1]);
            qT[0] = qT[0] + Convert.ToDouble(T_dat[2 * i]);
            qT[1] = qT[1] + m;
        }

        if (qT[1] >= min_reads && qC[1] >= min_reads)
        {
            //skipping output for yut file
            Locations.Add(cols[0] + "_" + cols[1]);
            num_snps++;
            double qC_hat = qC[0] / qC[1];
            double qT_hat = qT[0] / qT[1];
            double var_C = 1.0 / qC[1];
            double var_T = 1.0 / qT[1];

推荐答案

当你不明白你的代码在做什么或为什么它做它做的时候,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


代码在功能上是等价的。你说你的问题是qc [0]通常是0并且它导致问题。使用您的代码,您可以使用一些C_dat数组并将该数组的每个EVEN索引(0,2,4等)转换为double并将其添加到qc [0]。所以qc [0]应该等于0的唯一原因是,如果C_dat的每个偶数索引都是0.你应该查看你的代码,看看为什么会发生这种情况。
The code is functionally equivalent. You say that your problem is that qc[0] is often 0 and its causing problems. With your code your taking some C_dat array and converting every EVEN index of that array (0,2,4,etc) into a double and adding it to qc[0]. So the only reason qc[0] should equal 0 is if every even index of C_dat is 0. You should look into your code and see why that is happening.


这篇关于我可以在C#中获得一些与Java数组等效的帮助吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆