如何在单个Java方法中返回int和string属性?
我想从如何在单个Java方法中返回int和string属性?,java,arrays,methods,return,Java,Arrays,Methods,Return,我想从BaseballPlayer类返回所有属性的值。需要执行此操作的方法必须是公共字符串getBaseballPlayer(int i)方法(因为我需要在getBaseballPlayers()中引用此方法,以将所有值作为字符串的数组列表返回),因为所有属性都具有不同的数据类型,所以执行此操作时遇到问题(int,String,Height) 我试过这样做: public String getBaseballPlayer(int i){ ArrayList <String> bAr
BaseballPlayer
类返回所有属性的值。需要执行此操作的方法必须是公共字符串getBaseballPlayer(int i)
方法(因为我需要在getBaseballPlayers()
中引用此方法,以将所有值作为字符串的数组列表返回),因为所有属性都具有不同的数据类型,所以执行此操作时遇到问题(int
,String
,Height
)
我试过这样做:
public String getBaseballPlayer(int i){
ArrayList <String> bArray = new ArrayList <String>();
bArray.add(getHometown());
bArray.add(getState());
bArray.add(getHighSchool());
bArray.add(getPosition());
公共字符串getBaseballPlayer(int i){
ArrayList bArray=新的ArrayList();
add(gethomely());
add(getState());
添加(getHighSchool());
add(getPosition());
但是,它只适用于字符串方法,不一定返回实际值,而是返回每个字符串属性的get方法
public class BaseballPlayer extends Player implements Table {
private int num;
private String pos;
public BaseballPlayer( int a, String b, String c, int d,
String e, String f, String g, Height h){
super(a,ft,in,c,d,e,f,ht);
num = a;
pos = b;
}
public BaseballPlayer(){}
//Returns the value of a specific attribute. The input parameter start
with 0 for the first attribute, then 1 for the second attribute and so
on.
//you can use getBaseballPlayer(int i) in getBaseballPlayers( ) with a for
loop getting each getBaseballPlayer(int i).
public String getBaseballPlayer(int i){
ArrayList <String> bArray = new ArrayList <String>();
bArray.add(getHometown());
bArray.add(getState());
bArray.add(getHighSchool());
bArray.add(getPosition());
return (bArray);
}
//Returns the value of all attributes as an ArrayList of Strings.
public ArrayList <String> getBaseballPlayers(){
}
公共类BaseballPlayer扩展播放器实现表{
私有整数;
专用字符串pos;
公共棒球手(a、b、c、d、,
字符串e、字符串f、字符串g、高度h){
超级(a、ft、in、c、d、e、f、ht);
num=a;
pos=b;
}
公共棒球手(){}
//返回特定属性的值。输入参数开始
第一个属性为0,第二个属性为1,依此类推
在…上
//您可以在getBaseballPlayer()中使用getBaseballPlayer(inti)和for
循环获取每个getBaseballPlayer(int i)。
公共字符串getBaseballPlayer(int i){
ArrayList bArray=新的ArrayList();
add(gethomely());
add(getState());
添加(getHighSchool());
add(getPosition());
返回(营房);
}
//以字符串的ArrayList形式返回所有属性的值。
公共阵列列表GetBaseballPlayer(){
}
我只是在寻找返回每个属性值的最简单方法,然后使用该方法在另一个方法中将每个值作为字符串的arraylist返回。将整个对象作为一个字符串返回不是一个好做法。除非您被迫这样做,否则不要尝试这样做 好的,如果您的需求无法更改,并且如果您希望棒球对象中的所有内容都在一个字符串中,那么您可以将所有参数连接到一个分隔符,如“:” 例如:
在调用端,可以使用“split()”从该字符串中获取各个值字符串的方法。如果你想做得完美,你想做的是什么。它在简单JSON的基础上,可以将任意类、数据结构和其他类型编码成JSON,这样你就可以从JSON表示中轻松地重建这些对象。考虑到它的实际功能有多强大,使用起来很容易 如果您不需要对JSON本身无法处理的类型进行编码,那么使用常规JSON就更容易了。在您的情况下,这似乎已经足够好了。JSON的好处在于它是一个标准。您不必选择编码方案,而且您已经有了用任何您能想到的语言编写的库,可以阅读您的字符串已转换为数据 下面是一个大致遵循代码所做操作的示例:
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
public class BaseballPlayer {
private String name;
private String hometown;
private String state;
private int age;
private double height;
private String position;
static ObjectMapper mapper = new ObjectMapper();
public BaseballPlayer( String name, String hometown, String state, int age, double height, String position) {
this.name = name;
this.hometown = hometown;
this.state = state;
this.age = age;
this.height = height;
this.position = position;
}
public void setName(String name) {
this.name = name;
}
public void setHometown(String hometown) {
this.hometown = hometown;
}
public void setState(String state) {
this.state = state;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(float height) {
this.height = height;
}
public void setPosition(String position) {
this.position = position;
}
public String toString() {
return String.format("Name: %s from %s, %s (height: %.1f)", name, hometown, state, height);
}
public BaseballPlayer(){}
// Turn a BaseballPlayer object into a String
public String getAsJSON() {
Map<String, Object> info = new HashMap<>();
info.put("name", name);
info.put("hometown", hometown);
info.put("state", state);
info.put("age", age);
info.put("height", height);
info.put("position", position);
try {
return mapper.writeValueAsString(info);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// Here's the method you ask for. I don't know what 'i' is supposed
// to do, since we're in the class for a single baseball player. You
// could create a class that contains a list of baseball players, but
// why not just use a List by itself, as I've done.
public String getBaseballPlayer(int i) {
return getAsJSON();
}
// Turn a list of BaseballPlayer objects into a list of Strings
public static List<String> playersToStrings(List<BaseballPlayer> players) {
List<String> r = new ArrayList<>();
for (BaseballPlayer player : players) {
r.add(player.getAsJSON());
}
return r;
}
// Turn a list of Strings into a list of BaseballPlayer objects
public static List<BaseballPlayer> stringsToPlayers(List<String> playerStrings) {
List<BaseballPlayer> r = new ArrayList<>();
for (String playerString : playerStrings) {
try {
BaseballPlayer player = mapper.readValue(playerString, BaseballPlayer.class);
r.add(player);
} catch (IOException e) {
e.printStackTrace();
}
}
return r;
}
public static void main(String... args) {
// Create a list of BaseballPlayer objects and print them
List<BaseballPlayer> players = new ArrayList<>();
players.add(new BaseballPlayer("Joe", "Boston", "MA", 25, 6.1, "First Base"));
players.add(new BaseballPlayer("Sam", "San Francisco", "CA", 28, 5.8, "Pitcher"));
players.add(new BaseballPlayer("Kelly", "Chicago", "IL", 32, 6.4, "Catcher"));
System.out.println(players);
// Convert the list to a list of Strings and print the list
List<String> playerStrings = playersToStrings(players);
System.out.println(playerStrings);
// Convert the Strings back into BaseballPlayer objects and print them
players = stringsToPlayers(playerStrings);
System.out.println(players);
}
}
在这里,每个球员都被单独转换成JSON。再多几行代码,您就可以将棒球运动员对象数组转换成单个字符串
如果这个仅JSON的解决方案对您来说还不够好,请查看Gson。它可以保留所有Java类型。只需再进行一点设置,就可以描述如何将每个对象转换为JSON并返回。IMO最简单的选择是将它们包装到一个对象中,然后返回!我没有否决,但您不应该使用
I
所以mewhere?Hello@GBlodgett。这个问题只是给出了所需内容的粗略概念。它甚至不会编译。因此,我只想画出相同的图片,以便提问者能够轻松理解。我真的不知道为什么有人必须对问题和答案投反对票。我已经清楚地提到,这不是一个好的实践将所有内容都转换为字符串。但如果他的要求根本无法更改,则别无选择。我们应该提供一种实现方法。:)许多人会认为,如果问题只是给出了所需内容的粗略概念(即要求不明确)被提问的问题应该被关闭,而不是被回答。请参阅讨论的主题谢谢你强调@GBlodgett.我是新来的。因此,在回答之前,我可能需要了解交易。无论如何,有人提到,如果对坏问题有一个好的答案,就不应该被否决。接下来的答案不应该被否决d、 不管怎么说,既然不是你,你就无能为力了。谢谢你的帮助。没问题。我不会让否决票打扰你。如果你继续做出有益的贡献,你会得到更多的选票和声誉。
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
public class BaseballPlayer {
private String name;
private String hometown;
private String state;
private int age;
private double height;
private String position;
static ObjectMapper mapper = new ObjectMapper();
public BaseballPlayer( String name, String hometown, String state, int age, double height, String position) {
this.name = name;
this.hometown = hometown;
this.state = state;
this.age = age;
this.height = height;
this.position = position;
}
public void setName(String name) {
this.name = name;
}
public void setHometown(String hometown) {
this.hometown = hometown;
}
public void setState(String state) {
this.state = state;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(float height) {
this.height = height;
}
public void setPosition(String position) {
this.position = position;
}
public String toString() {
return String.format("Name: %s from %s, %s (height: %.1f)", name, hometown, state, height);
}
public BaseballPlayer(){}
// Turn a BaseballPlayer object into a String
public String getAsJSON() {
Map<String, Object> info = new HashMap<>();
info.put("name", name);
info.put("hometown", hometown);
info.put("state", state);
info.put("age", age);
info.put("height", height);
info.put("position", position);
try {
return mapper.writeValueAsString(info);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
// Here's the method you ask for. I don't know what 'i' is supposed
// to do, since we're in the class for a single baseball player. You
// could create a class that contains a list of baseball players, but
// why not just use a List by itself, as I've done.
public String getBaseballPlayer(int i) {
return getAsJSON();
}
// Turn a list of BaseballPlayer objects into a list of Strings
public static List<String> playersToStrings(List<BaseballPlayer> players) {
List<String> r = new ArrayList<>();
for (BaseballPlayer player : players) {
r.add(player.getAsJSON());
}
return r;
}
// Turn a list of Strings into a list of BaseballPlayer objects
public static List<BaseballPlayer> stringsToPlayers(List<String> playerStrings) {
List<BaseballPlayer> r = new ArrayList<>();
for (String playerString : playerStrings) {
try {
BaseballPlayer player = mapper.readValue(playerString, BaseballPlayer.class);
r.add(player);
} catch (IOException e) {
e.printStackTrace();
}
}
return r;
}
public static void main(String... args) {
// Create a list of BaseballPlayer objects and print them
List<BaseballPlayer> players = new ArrayList<>();
players.add(new BaseballPlayer("Joe", "Boston", "MA", 25, 6.1, "First Base"));
players.add(new BaseballPlayer("Sam", "San Francisco", "CA", 28, 5.8, "Pitcher"));
players.add(new BaseballPlayer("Kelly", "Chicago", "IL", 32, 6.4, "Catcher"));
System.out.println(players);
// Convert the list to a list of Strings and print the list
List<String> playerStrings = playersToStrings(players);
System.out.println(playerStrings);
// Convert the Strings back into BaseballPlayer objects and print them
players = stringsToPlayers(playerStrings);
System.out.println(players);
}
}
[Name: Joe from Boston, MA (height: 6.1), Name: Sam from San Francisco, CA (height: 5.8), Name: Kelly from Chicago, IL (height: 6.4)]
[{"hometown":"Boston","name":"Joe","state":"MA","position":"First Base","age":25,"height":6.1}, {"hometown":"San Francisco","name":"Sam","state":"CA","position":"Pitcher","age":28,"height":5.8}, {"hometown":"Chicago","name":"Kelly","state":"IL","position":"Catcher","age":32,"height":6.4}]
[Name: Joe from Boston, MA (height: 6.1), Name: Sam from San Francisco, CA (height: 5.8), Name: Kelly from Chicago, IL (height: 6.4)]