tostring

ToString() function in Go

房东的猫 提交于 2019-12-29 03:11:09
问题 The strings.Join function takes slices of strings only: s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) But it would be nice to be able to pass arbitrary objects which implement a ToString() function. type ToStringConverter interface { ToString() string } Is there something like this in Go or do I have to decorate existing types like int with ToString methods and write a wrapper around strings.Join ? func Join(a []ToStringConverter, sep string) string 回答1: Attach a

toString(), equals(), and hashCode() in an interface

白昼怎懂夜的黑 提交于 2019-12-28 11:48:09
问题 So, I have an interface with a bunch of methods that need to be implemented, the method names are irrelevant. The objects that implement this interface are often put into collections, and also have a special toString() format that I want them to use. So, I thought it would be convenient to put hashCode(), equals(), and toString() into the interface, to make sure that I remember to override the default method for these. But when I added these methods to the interface, the IDE/Compiler doesn't

JavaAPI-常用方法重写

泄露秘密 提交于 2019-12-26 22:49:57
Object中常见的重写方法 toString()方法 System.out.println(Object o) 该方法给对象的toString方法返回的字符串输出到控制台。 toString()方法 1.Object的 toString 方法是常见的会被子类重写的方法,该方法原始定义返回的字符串是对象的“句柄”,即地址信息,但是实际价值小。 该方法很少会直接调用,都是在调用某个API时被自动调用。 toString方法返回的字符串没有严格的格式要求,可结合实际情况而定,但是通常该字符串应当包括当前对象的属性信息。 示例: /** 重写toString()方法,返回x,y的坐标(x,y) */ public String toString ( ) { return "(" + x + "," + y + ")" ; } equals()方法 Object中equals方法的源码是这样的: public boolean equals ( Object obj ) { return ( this == obj ) ; } 对于引用类型而言: = =的比较是比较两个变量的值,即地址是否相同== ,因此 = =的意义是比较两个变量是否指向“同一个对象”。 equals方法是Object提供的方法,设计意义是比较两个变量指向的对象“内容是否相同”。但是该方法需要被子类重写,否则就是比较==

How to print contents from 2 different arrays and have items grouped

女生的网名这么多〃 提交于 2019-12-25 09:00:20
问题 I'm trying to output a list of all the clubs along with the people entered for each club, but have it display each club individually (i.e. there is one club and the list of people for that club, then the second club entered and the people for that one and so on) I wanted to make sure the adding of objects to each array was correct and figure out what my toString() method should look like. Here's my code so far: public class app { public static Club[] clubArray = new Club[5]; public static int

Try to convert Arraylist<LatLng> to string before insert to database

半世苍凉 提交于 2019-12-25 08:06:51
问题 I try to insert my ArrayList<LatLng> list1 with a lot of values like this: (99.9999999,99.9999999) to table in database MySQL, exacly to 1 column, something like this: row 1 (99.9999999,99.9999999) row 2 (99.9999999,99.9999999) row 3 (99.9999999,99.9999999) ... all to 1 column. In my opinion, currently i have a good method for this: String sql = "INSERT INTO table1 VALUES("; for(String s : list1) { sql = sql+"'"+s+"'"; } sql = sql+")"; stmt.executeUpdate(sql); but Android Studio underlines

toString an arraylist containing object

混江龙づ霸主 提交于 2019-12-25 07:12:48
问题 I have an array list of type car. I have an overriding toString method which prints my car in the desired format. I'm using the arraylist.get(index) method to print the cars. I only have one for now it works, but I want it do print for all of the cars in the array list. This is my code: public class Garage { private static int carsCapacity = 30; ArrayList<Cars> myGarage; public Garage() { Scanner scanAmountOfCars = new Scanner(System.in); System.out.println("Please limit the number of car the

Number values changing when toString() applied

落爺英雄遲暮 提交于 2019-12-25 06:59:05
问题 I am noticing something very strange when testing one of my functions that checks the validity of numbers, I first noticed it when testing the 'number.toString()[0]'. If I console logged (fLetter(019272)); the '0' is always showing '1'. After checking online I couldn't find any issues associated with using the toString() method on numbers containing '0'. function numberValidate(number) { var numCheck = [0, 5, 7, 8, 9]; var fLetter = number.toString()[0]; if (number.match(/^\d{6}$/)) { for

How to Iterate through LinkedList that contains Object - Java

五迷三道 提交于 2019-12-25 05:32:11
问题 How can i iterate through my LinkedList which i added Objects? private LinkedList sensors = new LinkedList(); enter code here //Constructor for my class is taking three arguments public Sensor(int ID, String Name, String comments) { this.ID = ID; this.NAME = Name; this.COMMENTS = comments; } //Then I query the database to get the values of the variables to create a new Object ID = Integer.parseInt(dbResult.getString("sensorID") ); NAME = dbResult.getString("sensorName"); COMMENTS = dbResult

How to Iterate through LinkedList that contains Object - Java

纵然是瞬间 提交于 2019-12-25 05:32:04
问题 How can i iterate through my LinkedList which i added Objects? private LinkedList sensors = new LinkedList(); enter code here //Constructor for my class is taking three arguments public Sensor(int ID, String Name, String comments) { this.ID = ID; this.NAME = Name; this.COMMENTS = comments; } //Then I query the database to get the values of the variables to create a new Object ID = Integer.parseInt(dbResult.getString("sensorID") ); NAME = dbResult.getString("sensorName"); COMMENTS = dbResult

printing out objects within objects stored in different arrays using toString (java)

最后都变了- 提交于 2019-12-25 03:01:55
问题 I'm having the most difficult time trying to output a list of all clubs and have the people who are in that club displayed under it, but it displays all people for each club instead. I know the problem lies within the nested for loop because when it gets to the second for loop, it goes to the personArray with a new index value and prints out all the people. How do I get it so that it prints only the contents of clubArray[0] , then for clubArray[1] it will pick up where it left off in the