我有一个固定大小(10)的类型int数组,它没有完全填满。
我想计算数组中有多少个整数,这样我就知道如果我想在数组中添加一个整数,那么将数组索引到什么值。 (这样我就不会替换另一个值)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testMultipleDDLpopulate.aspx.cs" Inherits="ABCFieldTicketWebApp.testMultipleDDLpopulate" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlRigYard" runat="server" DataTextField="EquipNumber" DataValueField="EquipNumber" AutoPostback="True" OnSelectedIndexChanged="ddlRigYard_SelectedIndexChanged" Height="16px" Width="196px"></asp:DropDownList>
<asp:SqlDataSource ID="dsRigYard" runat="server" ConnectionString="<%$ ConnectionStrings:LESFieldTicketConnectionString %>" SelectCommand="SELECT [EquipNumber], [LocationMovedTo], [NAVLocationCode], [NAVLocal2] FROM [VW_LES_EquipCurrentLocation]"></asp:SqlDataSource>
<asp:TextBox ID="txtLocationMovedTo" runat="server"></asp:TextBox>
<asp:TextBox ID="txtNAVLocationCode" runat="server"></asp:TextBox>
<asp:TextBox ID="txtNAVLocal2" runat="server"></asp:TextBox>
</div>
<div>
<asp:DropDownList ID="ddlResourceCode" runat="server" DataTextField="[Name]" DataValueField="[No_]" AutoPostback="True" OnSelectedIndexChanged="ddlResourceCode_SelectedIndexChanged" Height="16px" Width="196px"></asp:DropDownList>
<asp:SqlDataSource ID="dsResourceCode" runat="server" ConnectionString="<%$ ConnectionStrings:LESFieldTicketConnectionString %>" SelectCommand="SELECT [Name], [No_], [Unit Price] AS Unit_Price, [Base Unit Of Measure] AS Base_Unit_Of_Measure, [Resource Group No_] AS Resource_Group_No_, [LOB], [MajorAcct] FROM [VW_LES_NAVResourceCode]"></asp:SqlDataSource>
<asp:TextBox ID="txtNo" runat="server"></asp:TextBox>
<asp:TextBox ID="txtUnitPrice" runat="server"></asp:TextBox>
<asp:TextBox ID="txtUOM" runat="server"></asp:TextBox>
<asp:TextBox ID="txtResourceGrp" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLOB" runat="server"></asp:TextBox>
<asp:TextBox ID="txtMajorAcct" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace ABCFieldTicketWebApp
{
public partial class testMultipleDDLpopulate : System.Web.UI.Page
{
protected void Page_Load1(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateDropDownList1();
}
}
protected void Page_Load2(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateDropDownList2();
}
}
private void PopulateDropDownList1()
{
string constr1 = ConfigurationManager.ConnectionStrings["LESFieldTicketConnectionString"].ConnectionString;
using (SqlConnection con1 = new SqlConnection(constr1))
{
using (SqlCommand cmd1 = new SqlCommand("SELECT EquipNumber FROM VW_LES_EquipCurrentLocation", con1))
{
using (SqlDataAdapter da1 = new SqlDataAdapter(cmd1))
{
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1);
this.ddlRigYard.DataTextField = "EquipNumber";
this.ddlRigYard.DataValueField = "EquipNumber";
this.ddlRigYard.DataSource = ds1;
this.ddlRigYard.DataBind();
this.ddlRigYard.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Please Select", "0"));
}
}
}
}
protected void ddlRigYard_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = ddlRigYard.SelectedItem.Value;
string constr1 = ConfigurationManager.ConnectionStrings["LESFieldTicketConnectionString"].ConnectionString;
using (SqlConnection con1 = new SqlConnection(constr1))
{
SqlCommand command1 = new SqlCommand("SELECT [EquipNumber], [LocationMovedTo], [NAVLocationCode], [NAVLocal2] FROM [VW_LES_EquipCurrentLocation] WHERE EquipNumber= @EquipNumber", con1);
command1.Parameters.AddWithValue("@EquipNumber", selected);
command1.CommandType = CommandType.Text;
con1.Open();
SqlDataReader reader = command1.ExecuteReader();
using (reader)
{
if (reader.HasRows)
{
reader.Read();
//add as many as needed to fill your textboxes
txtLocationMovedTo.Text = reader.GetString(1);
txtNAVLocationCode.Text = reader.GetString(2);
txtNAVLocal2.Text = reader.GetString(3);
}
else { }
}
}
}
private void PopulateDropDownList2()
{
string constr2 = ConfigurationManager.ConnectionStrings["LESFieldTicketConnectionString"].ConnectionString;
using (SqlConnection con2 = new SqlConnection(constr2))
{
using (SqlCommand cmd2 = new SqlCommand("SELECT [Name], [No_] FROM [VW_LES_NAVResourceCode]", con2))
{
using (SqlDataAdapter da2 = new SqlDataAdapter(cmd2))
{
con2.Open();
DataSet ds2 = new DataSet();
da2.Fill(ds2);
this.ddlRigYard.DataTextField = "[Name]";
this.ddlRigYard.DataValueField = "[No_]";
this.ddlRigYard.DataSource = ds2;
this.ddlRigYard.DataBind();
this.ddlRigYard.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Please Select", "0"));
}
}
}
}
protected void ddlResourceCode_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = ddlResourceCode.SelectedItem.Value;
string constr2 = ConfigurationManager.ConnectionStrings["LESFieldTicketConnectionString"].ConnectionString;
using (SqlConnection con2 = new SqlConnection(constr2))
{
SqlCommand command2 = new SqlCommand("SELECT [Name], [No_], [Unit Price] AS Unit_Price, [Base Unit Of Measure] AS Base_Unit_Of_Measure, [Resource Group No_] AS Resource_Group_No_, [LOB], [MajorAcct] FROM [VW_LES_NAVResourceCode] WHERE [No_]=@[No_]", con2);
command2.Parameters.AddWithValue("@[No_]", selected);
command2.CommandType = CommandType.Text;
con2.Open();
SqlDataReader reader = command2.ExecuteReader();
using (reader)
{
if (reader.HasRows)
{
reader.Read();
//add as many as needed to fill your textboxes
txtNo.Text = reader.GetString(1);
txtUnitPrice.Text = reader.GetString(2);
txtUOM.Text = reader.GetString(3);
txtResourceGrp.Text = reader.GetString(4);
txtLOB.Text = reader.GetString(5);
txtMajorAcct.Text = reader.GetString(6);
}
else { }
}
}
}
}
}
当我输出变量计数器时,我得到值“0”。
我查看了几个Stack Overflow帖子,我似乎找到了一些可以帮助我找到解决方案的东西!任何帮助或建议都会有很大的帮助。
感谢。
修改 我能够得到我的问题的答案,但我后悔发帖因为我现在有2个声望点!你们这些人很苛刻! 这是正确的答案
//Define Array
int counter=0;
int scores[10] = {92,87,94,99,96};
//Count how many elements are in the score array
for (int i = 0; i < 10; i++)
{
if (scores[i] == 1)
{
counter++;
}
}
答案 0 :(得分:3)
例如,您可以认为包含0(或-1)的数组元素是未填充的元素。
在任何情况下,数组的每个元素都应包含实际值或0。
此初始化
int scores[10] = {92,87,94,99,96};
相当于
int scores[10] = {92,87,94,99,96,0,0,0,0,0};
所以循环看起来像
for ( size_t i = 0; i < sizeof( scores ) / sizeof( *scores ); i++ )
{
if ( scores[i] != 0)
{
counter++;
}
}
如果要对非填充元素使用值-1,则必须显式初始化非填充元素,例如
int scores[10] = {92,87,94,99,96,-1,-1,-1,-1,-1};
并且循环看起来像
for ( size_t i = 0; i < sizeof( scores ) / sizeof( *scores ); i++ )
{
if ( scores[i] != -1)
{
counter++;
}
}
答案 1 :(得分:0)
你不必做所有这些。 每次在数组中存储数字时,只需递增计数器,计数器本身就会告诉您需要存储下一个数字的索引。
答案 2 :(得分:0)
使用std::vector
,然后使用size
方法:
std::vector v={1,2,4,8};
cout << "Vector size is: " << v.size() << "\n"