我正在尝试创建一个看起来像
的main_map[1:[gender:m, age:1, name:a]].
我试图通过创建一个如下所示的临时地图(student_map)来实现这一目标
[gender:m, age:1, name:a]
我希望将student_map用作另一个地图(main_map)的值,该地图的键值为count(用于序列号),看起来像
[1:[gender:m, age:1, name:a]]
如果我运行下面的脚本并输入3个条目,则main_map显示计数递增3但最后输入的元素迭代3次,即使student_map的值正在改变,我得到的最终结果是最后一个条目student_map迭代了我输入的次数。例如。当我运行我的代码并通过命令提示符输入值。
1st Entry(through command prompt)
name=a
age=1
gender=m
2nd Entry
name=b
age=2
gender=m
3rd entry
name=c
age=3
gender=m
预期产出
[1:[gender:m, age:1, name:a],2:[gender:m, age:2, name:b],3:[gender:m, age:3, name:c]]
输出我正在
[1:[gender:m, age:3, name:c], 2:[gender:m, age:3, name:c], 3:[gender:m, age:3, name:c]]
注意:我无法使用以下一行代码
在此地图中搜索特定值def result=main_map.find{it.value.name == 'a'} \\This is present in both pieces of code below \\
我已在下面粘贴了两段代码(两者都有效但未按预期进行)
代码1 - 这给了我意想不到的输出
def answer=System.console().readLine 'Do you want to add a student Y/N\n'
def student_map = new HashMap()
//print student_map.getClass()
def main_map = [:]
//print main_map.getClass()
def count=0
while (answer=='Y' || answer=='y') {
count++
def name=System.console().readLine 'What is your name\n'
def age=System.console().readLine 'What is your age\n'
def gender=System.console().readLine 'What is your gender M/F\n'
student_map.put('name',"${name}")
student_map.put('age',"${age}")
student_map.put('gender',"${gender}")
print student_map
main_map.put("${count}","${student_map}")
print main_map
answer=System.console().readLine 'Do you want to continueY/N\n'
}
print main_map
def name='a'
def result=main_map.find{it.value.name == 'a'}
代码2 - 这给了我预期的输出,但没有运气搜索值
def answer=System.console().readLine 'Do you want to add a student Y/N\n'
def student_map = new HashMap()
//print student_map.getClass()
def main_map = [:]
//print main_map.getClass()
def count=0
while (answer=='Y' || answer=='y') {
count++
def name=System.console().readLine 'What is your name\n'
def age=System.console().readLine 'What is your age\n'
def gender=System.console().readLine 'What is your gender M/F\n'
student_map=[name:(name),age:(age),gender:(gender)]
print student_map
main_map.put("${count}","${student_map}")
print main_map
answer=System.console().readLine 'Do you want to continueY/N\n'
}
print main_map
def name='a'
def result=main_map.find{it.value.name == 'a'}
感谢我能得到的任何帮助
感谢vahidreza的回复。我已经纠正了那个部分,现在却把这个地图放到了一个文件中。 我的脚本下面的行有问题。虽然我可以插入键或值
,但我无法插入键和值工作
main_map.each {k, v -> writer.print(k)}
o/p from file
12 (i entered two records so the count incremented twice and hence 1 and 2)
不工作
main_map.each {k, v -> writer.print(k,v)}
完整脚本
def answer=System.console().readLine 'Do you want to add a student Y/N\n'
def student_map = new HashMap()
//print student_map.getClass()
def main_map = [:]
//print main_map.getClass()
def count=0
while (answer=='Y' || answer=='y') {
count++
def name=System.console().readLine 'What is your name\n'
def age=System.console().readLine 'What is your age\n'
def sex=System.console().readLine 'What is your sex M/F\n'
student_map=[name:(name),age:(age),sex:(sex)]
//print student_map
main_map.put((count),(student_map))
//print main_map
answer=System.console().readLine 'Do you want to continueY/N\n'
}
File f = new File ("C:/Apoorv/groovy-2.0.8/Groovy_programs/students.txt")
writer = new PrintWriter(f)
main_map.each {k, v -> writer.print(k,v)}
writer.close()
def student_name = System.console().readLine 'Name to search ?\n'
def result = main_map.find { it.value.name == "${student_name}" }
print result
感谢您的回复。我修复了无法正常工作/工作的部分。现在我在将数据放入文件并存储时遇到了一些问题。如前所述,我能够将特定记录的键或值存储在文件中,但我无法在文件中插入整个记录。
更新##我的代码工作正常,正在做我打算做的事情,但排序有一个奇怪的问题###
以下是我的参考资料
代码#
def answer=System.console().readLine 'Do you want to add a student Y/N\n'
def student_map = new HashMap()
//print student_map.getClass()
def main_map = [:]
//print main_map.getClass()
def count=0
while (answer=='Y' || answer=='y') {
count++
def name=System.console().readLine 'What is your name\n'
def age=System.console().readLine 'What is your age\n'
def sex=System.console().readLine 'What is your sex M/F\n'
student_map=[name:(name),age:(age),sex:(sex)]
//print student_map
main_map.put((count),(student_map))
//print main_map
answer=System.console().readLine 'Do you want to continueY/N\n'
}
print main_map.values().sort { a, b -> a.age <=> b.age}
File f = new File ("C:/Apoorv/groovy-2.0.8/Groovy_programs/students.txt") // Writing in file student.txt //
writer = new PrintWriter(f)
writer.print main_map
writer.close()
print main_map
def student_name = System.console().readLine 'Name to search ?\n'
def result = main_map.find { it.value.name == "${student_name}" }
print result
我想根据地图student_map的值对值进行排序。 我正在使用以下线来实现它
print main_map.values().sort { a, b -> a.age <=> b.age}
这适用但不适用于4位数的值。请考虑以下示例。
通过命令提示符给出的I / p
Do you want to add a student Y/N
What is your name
apoorv
What is your age
100
What is your sex M/F
m
Do you want to continueY/N
y
What is your name
kaandi
What is your age
21
What is your sex M/F
m
Do you want to continueY/N
y
What is your name
manjushree
What is your age
19
What is your sex M/F
f
Do you want to continueY/N
n
输出
[[name:apoorv, age:100, sex:m], [name:manjushree, age:19, sex:f], [name:kaandi, age:21, sex:m]]
正如您所看到的那样,第二个和第三个条目已经排序但不是第一个。我无法理解sort函数的这种行为。
任何帮助表示赞赏
答案 0 :(得分:1)
你的代码输出对我来说很好......我错过了什么吗?
def answer=System.console().readLine 'Do you want to add a student Y/N\n'
def student_map = new HashMap()
def main_map = [:]
def count=0
while (answer=='Y' || answer=='y') {
count++
def name=System.console().readLine 'What is your name\n'
def age=System.console().readLine 'What is your age\n'
def sex=System.console().readLine 'What is your sex M/F\n'
student_map=[name:(name),age:(age),sex:(sex)]
main_map.put((count),(student_map))
answer=System.console().readLine 'Do you want to continueY/N\n'
}
println main_map
C:\tmp>groovy run.groovy
Do you want to add a student Y/N
y
What is your name
a
What is your age
1
What is your sex M/F
m
Do you want to continueY/N
y
What is your name
b
What is your age
2
What is your sex M/F
m
Do you want to continueY/N
y
What is your name
c
What is your age
3
What is your sex M/F
f
Do you want to continueY/N
n
[1:[name:a, age:1, sex:m], 2:[name:b, age:2, sex:m], 3:[name:c, age:3, sex:f]]
输出看起来正确,对吗?你在ide中运行代码吗?