新数组每个循环迭代(New Array Every Loop Iteration)
我需要在循环的每次迭代中声明一个新数组,我知道我可以通过以下方式执行:
//Store mappings from array name (String) to int arrays (int[]) Map<String, int[]> namedArrays = new HashMap<String, int[]>(); for (int i = 0; i < 3; i++) { //This is going to be the name of your new array String arrayName = String.valueOf(i); //Map an new int[] to this name namedArrays.put(arrayName, new int[3]); } //If you need to access array called "2" do int[] array2 = namedArrays.get("2");
我的问题是在我声明一个新数组的部分,如果有一个已经命名为
ai[]
的数组,我可以像这样直接初始化它吗?:namedArrays.put(arrayName,ai);
我试过这个但是我收到了一个错误。
编辑:现在我明白我实际上有一个检索数组的问题我按以下方式初始化。
Map<String, int[]> namedArrays = new HashMap<String, int[]>(); for(int i=0;i<testCase;i++){ int num=scanner.nextInt(); int[] ai=new int[num]; for(int j=0;j<num;j++){ ai[j]=scanner.nextInt(); } String arrayName = String.valueOf(i); namedArrays.put(arrayName,ai);
我试图通过以下方式检索它
for(int i=0;i<testCase;i++){ int[] array2 = namedArrays.get(i);
array2包含类似“[I @ 1fc4bec”的值。 但它应该包含一个数组。
编辑2:我明白我可以通过以下方式获得阵列
String arrayName = String.valueOf(i); // System.out.println(namedArrays.get(arrayName)[0]); int b=namedArrays.get(arrayName).length; int[] array2=new int[b]; for(int z=0;z<b;z++){ array2[z] = namedArrays.get(arrayName)[z];}
但是,确实如此
array2[z] = namedArrays.get(arrayName)
回归“[I @ 1fc4bec”?
I need to declare a new array every iteration of the loop, I understand I can do it the following way:
//Store mappings from array name (String) to int arrays (int[]) Map<String, int[]> namedArrays = new HashMap<String, int[]>(); for (int i = 0; i < 3; i++) { //This is going to be the name of your new array String arrayName = String.valueOf(i); //Map an new int[] to this name namedArrays.put(arrayName, new int[3]); } //If you need to access array called "2" do int[] array2 = namedArrays.get("2");
My question is at the part where I declare a new array, what if have a array already named
ai[]
,can I initialize it directly like this?:namedArrays.put(arrayName,ai);
I tried this but I am getting an error.
EDIT: Now I understand I actually have a problem with retrieving the array I initialized the following way.
Map<String, int[]> namedArrays = new HashMap<String, int[]>(); for(int i=0;i<testCase;i++){ int num=scanner.nextInt(); int[] ai=new int[num]; for(int j=0;j<num;j++){ ai[j]=scanner.nextInt(); } String arrayName = String.valueOf(i); namedArrays.put(arrayName,ai);
I am trying to retrieve it the following way
for(int i=0;i<testCase;i++){ int[] array2 = namedArrays.get(i);
array2 contains value something like "[I@1fc4bec". But it is supposed to contain an array.
EDIT 2 : I understood that I can get the array the following way
String arrayName = String.valueOf(i); // System.out.println(namedArrays.get(arrayName)[0]); int b=namedArrays.get(arrayName).length; int[] array2=new int[b]; for(int z=0;z<b;z++){ array2[z] = namedArrays.get(arrayName)[z];}
But y does
array2[z] = namedArrays.get(arrayName)
return "[I@1fc4bec" ?
原文:https://stackoverflow.com/questions/38182869
最满意答案
-- This Section is Just to Stage some Sample Data -------------------------------------------------------------------------------------------------------- Declare @Inv table (SKU int,FirstReceiptDate Date,LastReceiptDate Date,OnHand int,SalesUnits int) Insert Into @Inv (SKU,FirstReceiptDate,LastReceiptDate,OnHand,SalesUnits) values (456,'2014-03-15','2014-12-14',0,15), (789,'2014-05-30','2014-12-15',10,35), (321,'2014-07-31','2016-03-16',112,60) Declare @Hier table (SKU int,PtSKU int,PS int,Title varchar(50)) Insert into @Hier (SKU,PtSKU,PS,Title) values (123,0,0,'SKU Tile 123'), (456,123,10,'Some Other Title SKU 456'), (789,123,20,'This is the Title for Title SKU 786'), (321,123,30,'Finally Tile 321') -- This Section Builds the Hierarchy with Range Keys Hierarchies can be variable depth -- My hierarchies are pretty static so they are rebuilt as needed -- The real power is using the range key. You can aggregate data without -- a recursive query. -- I should not that I added a Presentation Sequence (PS) and Title to the Hierarcy -- The PS is used to control the presentation order. This can be alphabetical as well -------------------------------------------------------------------------------------------------------- ;With cteOH (SKU,PtSKU,Lvl,PS,SortSeq) as ( Select SKU,PtSKU,Lvl=1,PS,cast([dbo].[udf-Str-PadL](PS,0,6) +':' +[dbo].[udf-Str-PadL](SKU,0,6) + '/' as varchar(500)) from @Hier where PtSKU=0 Union All Select h.SKU,h.PtSKU,cteOH.Lvl+1,h.PS,SortSeq=cast(cteOH.SortSeq + [dbo].[udf-Str-PadL](H.PS,0,6) +':' +[dbo].[udf-Str-PadL](H.SKU,0,6) + '/' as varchar(500)) FROM @Hier h INNER JOIN cteOH ON h.PtSKU = cteOH.SKU ) ,cteR1 as (Select SKU,SortSeq,R1=Row_Number() over (Order by SortSeq) From cteOH) ,cteR2 as (Select A.SKU,R2 = max(B.R1) From cteOH A Join cteR1 B on (B.SortSeq Like A.SortSeq+'%') Group By A.SKU) Select B.R1 ,C.R2 ,A.Lvl ,A.SKU ,A.PtSKU ,A.PS ,T.Title Into #TempOH From cteOH A Join cteR1 B on (A.SKU=B.SKU) Join cteR2 C on (A.SKU=C.SKU) Join @Hier T on (A.SKU=T.SKU) Order By B.R1 -- This Section illustrates how to aggregate data via the range keys -------------------------------------------------------------------------------------------------------- Select A.* ,FirstReceiptDate = min(B.FirstReceiptDate) ,LastReceiptDate = max(B.LastReceiptDate) ,OnHand = sum(B.OnHand) ,SalesUnits = sum(B.SalesUnits) ,MonthsSupply = cast(sum(B.OnHand*12.)/sum(B.SalesUnits) as money) ,FamilyFirstReceiptDate = First_Value(min(B.FirstReceiptDate)) Over (Order By A.R1) ,FamilyLastReceiptDate = First_Value(max(B.LastReceiptDate)) Over (Order By A.R1) ,FamilyOnHand = First_Value(sum(B.OnHand)) Over (Order By A.R1) ,FamilySalesUnits = First_Value(sum(B.SalesUnits)) Over (Order By A.R1) ,FamilyMonthsSupply = First_Value(cast(sum(B.OnHand*12.)/sum(B.SalesUnits) as money)) Over (Order By A.R1) From #TempOH A Join (Select _R1=B.R1,A.* From @Inv A Join #TempOH B on A.SKU=B.SKU) B on _R1 between A.R1 and A.R2 Group By A.R1,A.R2,A.Lvl,A.SKU,A.PtSKU,A.PS,A.Title Order By A.R1
返回
R1 R2 Lvl SKU PtSKU PS Title FirstReceiptDate LastReceiptDate OnHand SalesUnits MonthsSupply FamilyFirstReceiptDate FamilyLastReceiptDate FamilyOnHand FamilySalesUnits FamilyMonthsSupply -------------------- -------------------- ----------- ----------- ----------- ----------- -------------------------------------------------- ---------------- --------------- ----------- ----------- --------------------- ---------------------- --------------------- ------------ ---------------- --------------------- 1 4 1 123 0 0 SKU Tile 123 2014-03-15 2016-03-16 122 110 13.3091 2014-03-15 2016-03-16 122 110 13.3091 2 2 2 456 123 10 Some Other Title SKU 456 2014-03-15 2014-12-14 0 15 0.00 2014-03-15 2016-03-16 122 110 13.3091 3 3 2 789 123 20 This is the Title for Title SKU 786 2014-05-30 2014-12-15 10 35 3.4286 2014-03-15 2016-03-16 122 110 13.3091 4 4 2 321 123 30 Finally Tile 321 2014-07-31 2016-03-16 112 60 22.40 2014-03-15 2016-03-16 122 110 13.3091
所需功能
CREATE FUNCTION [dbo].[udf-Str-PadL] (@Value varchar(50),@Pad varchar(10) = '0',@Len int = 10) -- Syntax : Select [dbo].[udf-Str-PadL](25,0,10) -- Syntax : Select [dbo].[udf-Str-PadL](25,'-',6) Returns varchar(50) AS BEGIN Return right(concat(Replicate(@Pad,@Len),@Value),@Len) END
-- This Section is Just to Stage some Sample Data -------------------------------------------------------------------------------------------------------- Declare @Inv table (SKU int,FirstReceiptDate Date,LastReceiptDate Date,OnHand int,SalesUnits int) Insert Into @Inv (SKU,FirstReceiptDate,LastReceiptDate,OnHand,SalesUnits) values (456,'2014-03-15','2014-12-14',0,15), (789,'2014-05-30','2014-12-15',10,35), (321,'2014-07-31','2016-03-16',112,60) Declare @Hier table (SKU int,PtSKU int,PS int,Title varchar(50)) Insert into @Hier (SKU,PtSKU,PS,Title) values (123,0,0,'SKU Tile 123'), (456,123,10,'Some Other Title SKU 456'), (789,123,20,'This is the Title for Title SKU 786'), (321,123,30,'Finally Tile 321') -- This Section Builds the Hierarchy with Range Keys Hierarchies can be variable depth -- My hierarchies are pretty static so they are rebuilt as needed -- The real power is using the range key. You can aggregate data without -- a recursive query. -- I should not that I added a Presentation Sequence (PS) and Title to the Hierarcy -- The PS is used to control the presentation order. This can be alphabetical as well -------------------------------------------------------------------------------------------------------- ;With cteOH (SKU,PtSKU,Lvl,PS,SortSeq) as ( Select SKU,PtSKU,Lvl=1,PS,cast([dbo].[udf-Str-PadL](PS,0,6) +':' +[dbo].[udf-Str-PadL](SKU,0,6) + '/' as varchar(500)) from @Hier where PtSKU=0 Union All Select h.SKU,h.PtSKU,cteOH.Lvl+1,h.PS,SortSeq=cast(cteOH.SortSeq + [dbo].[udf-Str-PadL](H.PS,0,6) +':' +[dbo].[udf-Str-PadL](H.SKU,0,6) + '/' as varchar(500)) FROM @Hier h INNER JOIN cteOH ON h.PtSKU = cteOH.SKU ) ,cteR1 as (Select SKU,SortSeq,R1=Row_Number() over (Order by SortSeq) From cteOH) ,cteR2 as (Select A.SKU,R2 = max(B.R1) From cteOH A Join cteR1 B on (B.SortSeq Like A.SortSeq+'%') Group By A.SKU) Select B.R1 ,C.R2 ,A.Lvl ,A.SKU ,A.PtSKU ,A.PS ,T.Title Into #TempOH From cteOH A Join cteR1 B on (A.SKU=B.SKU) Join cteR2 C on (A.SKU=C.SKU) Join @Hier T on (A.SKU=T.SKU) Order By B.R1 -- This Section illustrates how to aggregate data via the range keys -------------------------------------------------------------------------------------------------------- Select A.* ,FirstReceiptDate = min(B.FirstReceiptDate) ,LastReceiptDate = max(B.LastReceiptDate) ,OnHand = sum(B.OnHand) ,SalesUnits = sum(B.SalesUnits) ,MonthsSupply = cast(sum(B.OnHand*12.)/sum(B.SalesUnits) as money) ,FamilyFirstReceiptDate = First_Value(min(B.FirstReceiptDate)) Over (Order By A.R1) ,FamilyLastReceiptDate = First_Value(max(B.LastReceiptDate)) Over (Order By A.R1) ,FamilyOnHand = First_Value(sum(B.OnHand)) Over (Order By A.R1) ,FamilySalesUnits = First_Value(sum(B.SalesUnits)) Over (Order By A.R1) ,FamilyMonthsSupply = First_Value(cast(sum(B.OnHand*12.)/sum(B.SalesUnits) as money)) Over (Order By A.R1) From #TempOH A Join (Select _R1=B.R1,A.* From @Inv A Join #TempOH B on A.SKU=B.SKU) B on _R1 between A.R1 and A.R2 Group By A.R1,A.R2,A.Lvl,A.SKU,A.PtSKU,A.PS,A.Title Order By A.R1
Returns
R1 R2 Lvl SKU PtSKU PS Title FirstReceiptDate LastReceiptDate OnHand SalesUnits MonthsSupply FamilyFirstReceiptDate FamilyLastReceiptDate FamilyOnHand FamilySalesUnits FamilyMonthsSupply -------------------- -------------------- ----------- ----------- ----------- ----------- -------------------------------------------------- ---------------- --------------- ----------- ----------- --------------------- ---------------------- --------------------- ------------ ---------------- --------------------- 1 4 1 123 0 0 SKU Tile 123 2014-03-15 2016-03-16 122 110 13.3091 2014-03-15 2016-03-16 122 110 13.3091 2 2 2 456 123 10 Some Other Title SKU 456 2014-03-15 2014-12-14 0 15 0.00 2014-03-15 2016-03-16 122 110 13.3091 3 3 2 789 123 20 This is the Title for Title SKU 786 2014-05-30 2014-12-15 10 35 3.4286 2014-03-15 2016-03-16 122 110 13.3091 4 4 2 321 123 30 Finally Tile 321 2014-07-31 2016-03-16 112 60 22.40 2014-03-15 2016-03-16 122 110 13.3091
Required function
CREATE FUNCTION [dbo].[udf-Str-PadL] (@Value varchar(50),@Pad varchar(10) = '0',@Len int = 10) -- Syntax : Select [dbo].[udf-Str-PadL](25,0,10) -- Syntax : Select [dbo].[udf-Str-PadL](25,'-',6) Returns varchar(50) AS BEGIN Return right(concat(Replicate(@Pad,@Len),@Value),@Len) END
相关问答
更多-
当父子关系在同一个表中时,一次插入多行(Inserting multiple rows at once when parent-child relationship is in the same table)[2024-01-21]
添加我的评论作为答案,以防为您做到这一点 我会用输出一个子句来追踪它。 TSQL输出首先插入所有父项并输出父名和parentid。 将子项暂存到#temp并使用输出表中的数据插入子项。 Added my comment as an answer in case that did it for you I would go after it with an output an clause. TSQL Output First insert all the parents and output the par ... -
如何附加“订单”字段? 这可能是一种方法: WITH ChildLocations(LocationId, FkParentLocationId, [Level]) AS ( ( -- Start CTE off by selecting the home location of the user SELECT l.LocationId, l.FkParentLocationId, 0 as [Level], ...
-
双重对齐(Double alignment)[2023-07-18]
一个扩展的评论: 根据GCC有关-malign-double文档: 在double字边界上对齐double变量产生的代码在Pentium上运行速度稍快,但代价是内存更多。 在x86-64上,默认情况下启用-malign-double 。 警告:如果使用-malign-double开关,则包含上述类型的结构的排列方式与386的发布应用程序二进制接口规范的排列方式不同,并且与没有使用该开关编译的代码中的结构不是二进制兼容的。 这里的一个词意味着i386字是32位。 即使在32位模式下,Windows也使用dou ... -
父子数据对齐(parent-child data alignment)[2023-09-22]
-- This Section is Just to Stage some Sample Data -------------------------------------------------------------------------------------------------------- Declare @Inv table (SKU int,FirstReceiptDate Date,LastReceiptDate Date,OnHand int,SalesUnits int) Ins ... -
虽然call-template和命名模板是该语言非常有用的功能,但如果您发现自己更喜欢apply-templates则可能表明您仍然在考虑函数而不是模板。 如果您在命名模板中执行的第一件事是选择要操作的节点集,则尤其如此。 这是您尝试做的简单版本。
对齐规则(Alignment Rules)[2021-08-22]
让我们打破这个: “对齐规则”,“要求每个原始数据元素的地址是元素大小的偶数倍”。 我们谈论对齐规则并不是很有趣; 我们已经知道了。 “要求每个原始数据元素的地址”是“元素大小的偶数倍”。 现在我们到了某个地方。 我们有一个要求和范围: Requirement: The address is an even multiple of the element's size. Scope: Every primitive data element. 因此,每次我们定位元素时,都必须强制要求。 让我们尝试将元素放 ...这就是我认为您的代码应该是什么样子 $json = json_decode($json); echo buildNavigation($json->Tasks); 产量奇怪的数据对齐(Strange data alignment)[2022-07-22]
在堆上分配时,不能对地址做出任何明确的陈述。 内存分配函数很可能维护有关已分配块的内联信息,这将影响后续块的地址,如: +--------+-------------+--------+-------------+ | header | alloced mem | header | alloced mem | ... +--------+-------------+--------+-------------+ 另外,为了提高效率,这些功能可能会将你的记忆集中到(例如)8或16的倍数(你仍然不允许使用它, ...数据对齐问题(Data Alignment Issue)[2023-07-01]
对齐的属性要求data元素位于8字节边界上。 为了确保结构数组的所有元素都正确对齐,整个结构必须是8字节对齐,并且通过使其长度为16字节来实现。 Your aligned attribute requires the data element to be on an 8-byte boundary. To ensure that all elements of an array of the structure are properly aligned, the structure as a whole h ...您可以使用SCOPE_IDENTITY()函数来检索上次插入的自动增量ID: DECLARE @new_parent_id INT INSERT INTO MyParent(...) VALUES(...) -- your new parent data SELECT @new_parent_id = SCOPE_IDENTITY() INSERT INTO MyChild(parent_id, ...) VALUES(@new_parent_id, ...) 你是这个意思吗? 编辑 : 您确实可以 ...相关文章
更多- 报错说找不到abbrev这个方法,但Array有这个方法的吧?
- Lua 迭代器iterator
- 怎么把2个数组里面相同的元素组合成一个新的数组
- Java 数组
- 动态拼接JSON数组的问题
- scala数组操作
- Lua循环遍历
- Lua 数组操作
- 找数组的波谷
- 新的开始
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)