类文本文件到数据字典(Textfile to class to Data Dictionary)
我正在尝试创建一个进入类的文本文件,并将该对象存储到数据字典中,然后创建一个允许我添加,编辑和删除等的GUI。
我的文本文件使用以下语法:
国家,GDP增长,通货膨胀,贸易平衡,HDI排名,主要贸易伙伴
例子:
- 美国,1.8,2,-3.1,4,[加拿大,英国,巴西]
- 加拿大,1.9,2.2,-2,6,[美国;中国]
无论如何,在我创建GUI之前,我试图让它在控制台中首先工作。 国家出现在控制台中但使用调试器中的步骤,我的对象数组和我的数据字典(如果我已正确创建数据字典)似乎是存储但是当它继续到下一个国家时它会覆盖前一个。 我怎样才能让所有国家都存储而不仅仅是存储。 如果这是有道理的,任何帮助将不胜感激。
我的代码:
class Program { static void Main(string[] args) { const int MAX_LINES_FILE = 50000; string[] AllLines = new string[MAX_LINES_FILE]; int i = 0; //reads from bin/DEBUG subdirectory of project directory AllLines = File.ReadAllLines(@"C:\Users\Jack\Documents\countries.csv"); country[] newCountry = new country[30]; foreach (string line in AllLines) { if (line.StartsWith("Country")) //found first line - headers { headers = line.Split(','); } else { string[] columns = line.Split(','); newCountry[i] = new country(); newCountry[i].Country=(columns[0]); newCountry[i].GDP=(columns[1]); newCountry[i].Inflation=(columns[2]); newCountry[i].TB =(columns[3]); newCountry[i].HDI =(columns[4]); newCountry[i].TP = (columns[5]); Dictionary<object, string> CountryList = new Dictionary<object, string>(); CountryList.Add(newCountry[i].Country, newCountry[i].GDP + "," + newCountry[i].Inflation + "," + newCountry[i].TB + "," + newCountry[i].HDI + "," + newCountry[i].TP); i++; foreach (KeyValuePair<object, string> country in CountryList) { Console.WriteLine("Country = {0}, GDP = {1}", country.Key, country.Value); } } Console.ReadKey(); } } public static string[] headers { get; set; } } public class country { public string Country { get; set; } public string GDP { get; set; } public string Inflation { get; set; } public string TB { get; set; } public string HDI { get; set; } public string TP { get; set; } }
}
编辑:按照建议移动CountryList仍然得到countryList计数保持1的相同问题。
我把它放在国家级的方法中。
public static void addD(country a) { Dictionary<object, string> CountryList = new Dictionary<object, string>(); CountryList.Add(a.Country, a.GDP); foreach (KeyValuePair<object, string> country in CountryList) { Console.WriteLine("Country = {0}, GDP = {1}", country.Key, country.Value); } }
并从这里调用它:
newCountry[i].TB =(columns[3]); newCountry[i].HDI =(columns[4]); newCountry[i].TP = (columns[5]); country.addD(newCountry[i]);
I'm trying to make a textfile that goes into a class and stores the object into a data dictionary and then create a GUI that allows me to add, edit and remove etc.
My text file is in the following syntax:
Country, GDP growth, Inflation, Trade Balance, HDI Ranking, Main Trade Partners
Examples:
- USA,1.8,2,-3.1,4,[Canada;UK;Brazil]
- Canada,1.9,2.2,-2,6,[USA;China]
Anyway before I create the GUI, I am trying make it work in the console first. The Countries come up in the console but using the step into in the debugger, my object array and my data dictionary (if I have created the data dictionary correctly) seem to be storing but though when it goes on to the next country it overwrites the previous one. How can I make it so that all countries get stored instead of just the one. If that makes sense any help will be greatly appreciated.
My code:
class Program { static void Main(string[] args) { const int MAX_LINES_FILE = 50000; string[] AllLines = new string[MAX_LINES_FILE]; int i = 0; //reads from bin/DEBUG subdirectory of project directory AllLines = File.ReadAllLines(@"C:\Users\Jack\Documents\countries.csv"); country[] newCountry = new country[30]; foreach (string line in AllLines) { if (line.StartsWith("Country")) //found first line - headers { headers = line.Split(','); } else { string[] columns = line.Split(','); newCountry[i] = new country(); newCountry[i].Country=(columns[0]); newCountry[i].GDP=(columns[1]); newCountry[i].Inflation=(columns[2]); newCountry[i].TB =(columns[3]); newCountry[i].HDI =(columns[4]); newCountry[i].TP = (columns[5]); Dictionary<object, string> CountryList = new Dictionary<object, string>(); CountryList.Add(newCountry[i].Country, newCountry[i].GDP + "," + newCountry[i].Inflation + "," + newCountry[i].TB + "," + newCountry[i].HDI + "," + newCountry[i].TP); i++; foreach (KeyValuePair<object, string> country in CountryList) { Console.WriteLine("Country = {0}, GDP = {1}", country.Key, country.Value); } } Console.ReadKey(); } } public static string[] headers { get; set; } } public class country { public string Country { get; set; } public string GDP { get; set; } public string Inflation { get; set; } public string TB { get; set; } public string HDI { get; set; } public string TP { get; set; } }
}
EDIT: Moved the CountryList as suggested still getting the same issue that the countryList count is staying on 1.
I have put it in a method in the country class.
public static void addD(country a) { Dictionary<object, string> CountryList = new Dictionary<object, string>(); CountryList.Add(a.Country, a.GDP); foreach (KeyValuePair<object, string> country in CountryList) { Console.WriteLine("Country = {0}, GDP = {1}", country.Key, country.Value); } }
and calling it from here:
newCountry[i].TB =(columns[3]); newCountry[i].HDI =(columns[4]); newCountry[i].TP = (columns[5]); country.addD(newCountry[i]);
原文:https://stackoverflow.com/questions/31565950
最满意答案
如果你需要一个
PlayerPlane
作为一个类,那么你可以这样做(因为PlayerPlane
是PlayerPlane
的一个子类,你可以使用它的初始值):class PlayerPlane: SKSpriteNode { static func populatePlayerPlane() -> PlayerPlane { let playerPlane = PlayerPlane(imageNamed: "airplane_1_14") playerPlane.position = CGPoint(x: 200, y: 200) playerPlane.zPosition = 50 return playerPlane } func fireEnemies() { print("Attack") } }
但是你也可以使用一个协议并为
SKSpriteNode
编写一个扩展,如果你想让其他对象/结构体采用PlayerPlane
功能,这很方便:protocol PlayerPlane { func fireEnemies() } extension SKSpriteNode : PlayerPlane { func fireEnemies() { print("Attack") } } extension SKSpriteNode { static func populatePlayerPlane() -> SKSpriteNode { let playerPlane = SKSpriteNode(imageNamed: "airplane_1_14") playerPlane.position = CGPoint(x: 200, y: 200) playerPlane.zPosition = 50 return playerPlane } }
最后,当你想以其他方式将
fireEnemies
功能附加到你的PlayerPlane
你还可以为协议写一个扩展名:class PlayerPlane: SKSpriteNode { static func populatePlayerPlane() -> PlayerPlane { let playerPlane = PlayerPlane(imageNamed: "airplane_1_14") playerPlane.position = CGPoint(x: 200, y: 200) playerPlane.zPosition = 50 return playerPlane } } protocol FireEnemies { func fireEnemies() } extension PlayerPlane : FireEnemies { func fireEnemies() { print("Attack") } }
If you need a
PlayerPlane
as a class then you can make it this way (asPlayerPlane
is a subclass ofSKSpriteNode
you can use it's initializers):class PlayerPlane: SKSpriteNode { static func populatePlayerPlane() -> PlayerPlane { let playerPlane = PlayerPlane(imageNamed: "airplane_1_14") playerPlane.position = CGPoint(x: 200, y: 200) playerPlane.zPosition = 50 return playerPlane } func fireEnemies() { print("Attack") } }
But you can also use a protocol and write an extension for
SKSpriteNode
which is convenient if you want to make other objects/structs adoptPlayerPlane
functionality:protocol PlayerPlane { func fireEnemies() } extension SKSpriteNode : PlayerPlane { func fireEnemies() { print("Attack") } } extension SKSpriteNode { static func populatePlayerPlane() -> SKSpriteNode { let playerPlane = SKSpriteNode(imageNamed: "airplane_1_14") playerPlane.position = CGPoint(x: 200, y: 200) playerPlane.zPosition = 50 return playerPlane } }
Finally, when you want "other" way to attach
fireEnemies
functionality to yourPlayerPlane
you can also write an extension for a protocol:class PlayerPlane: SKSpriteNode { static func populatePlayerPlane() -> PlayerPlane { let playerPlane = PlayerPlane(imageNamed: "airplane_1_14") playerPlane.position = CGPoint(x: 200, y: 200) playerPlane.zPosition = 50 return playerPlane } } protocol FireEnemies { func fireEnemies() } extension PlayerPlane : FireEnemies { func fireEnemies() { print("Attack") } }
相关问答
更多-
this并不总是像你认为的那样在箭头函数中运行。 胖箭头函数执行词法作用域(基本上查看周围的代码并根据上下文定义this 。) 如果您更改回常规回调函数表示法,您可能会获得所需的结果: UserSchema.methods.comparePassword = function(candidatePassword) { let candidateBuf = Buffer.from(candidatePassword, 'ascii'); if (sodium.crypto_pwhash_str_ver ...
-
据我所知,类的每个实例都存储对实例方法的引用。 我不知道你从哪里得到这个信息,但这是错误的。 他们不。 为什么实例方法不能以类似于实例属性的方式访问,即通过__dict__或通过其他系统属性? 那么,因为它们没有存储在实例中。 有什么方法可以查看(也许更改)实例方法的名称和引用吗? 由于这些引用不存在,您不能更改它们。 当然,您可以通过普通赋值创建任何您想要的属性,但请注意,存储在实例中的函数不会像普通方法那样处理 - 隐式传递self参数的机制不适用于它们。 From what I understand, ...
-
是否有可能通过类方法访问实例方法和变量(is it possible to access instance methods and variable via class methods)[2024-02-28]
这意味着这是允许的: public class Test { public int instanceVariable = 42; public void instanceMethod() {System.out.println("Hello!");} public static void staticMethod() { Test test = new Test(); System.out.println(test.instanceVariable ... -
类实例方法?(Class instance methods? object methods?)[2024-02-09]
尝试在目标C的上下文中更清楚地重述上述维基百科的引用: 类方法是属于类的方法,而不是类的实例。 实例方法是类实例的方法; 通常被称为对象。 Sayng“类实例方法”显然是指这一点,但令人困惑。 Trying to rephrase the above Wikipedia quote more clearly in the context of Objective C: Class methods are methods belonging to the class, rather than to an in ... -
如果对象方法嵌套在另一个对象实例中,如何访问它?(How to access an object methods if he is nested in another object instance?)[2024-01-22]
为了访问对象方法,您需要以某种方式以某种方式访问对象本身。 我建议你使用self在对象上添加一个包含Masonry实例的masonry属性,然后在那里传递构造的对象: //Warning: Untested code AnimOnScroll.prototype = { _init : function() { var self = this; imagesLoaded( this.el, function() { // initializ ... -
为什么instance_methods(false)返回祖先方法?(Why does instance_methods(false) return ancestor methods?)[2023-10-25]
我认为你没有考虑被覆盖的方法。 class A def foo 'A' end def bar 'baz' end end class B < A def foo super + 'B' end end A.instance_methods(false) #=> [:foo, :bar] B.instance_methods(false) #=> [:foo] 在这种情况下, A和B都有一个实例方法:foo在它们自己的级别,所以它们都会在它们实现的实例 ... -
如果你需要一个PlayerPlane作为一个类,那么你可以这样做(因为PlayerPlane是PlayerPlane的一个子类,你可以使用它的初始值): class PlayerPlane: SKSpriteNode { static func populatePlayerPlane() -> PlayerPlane { let playerPlane = PlayerPlane(imageNamed: "airplane_1_14") playerPlane.po ...
-
创建连接时(文件 - >连接对象资源管理器)...只需确保在“服务器类型”下拉列表中选择了Analysis Services ,而不是数据库引擎 。 When you create a connection (File -> Connect Object Explorer) ... just make sure that Analysis Services is selected in the "Server Type" drop down, instead of Database Engine.
-
无法连接到我的ec2实例(cant connect to my ec2 instance)[2022-04-05]
默认情况下,Linux AMI不包含Web服务器,因此在安装和启动Web服务器之前,您无法在其地址上看到网页。 您现在已经使用Tomcat完成了这项工作,显然您现在可以看到Tomcat所服务的网页。 The Linux AMI does not include a web server by default, so you cannot see a web page at its address until you install and start a web server. You've done th ... -
使用QSignalMapper和实例方法将PySide连接到信号(Connect PySide to a signal using QSignalMapper and an instance method)[2021-12-15]
我试图在PyQt中重现你的代码,但我并不完全了解Pyside和PyQt之间的差异,所以我的答案更多的是猜测。 尝试删除代码的第二行,并将最后一行替换为: self.mapper.mapped.connect(self.removeItem) I tried to reproduce your code in PyQt but I'm not fully aware of the differences between Pyside and PyQt so my answer is more of a gu ...