Java 创建合并两个对象字段的ArrayList?
我是Java新手,正在使用BlueJ 课程背景概述 我正在尝试创建一个俱乐部数据库(arraylist,而不是实际的数据库),用户可以在其中向arraylist添加一个新的登山者(姓名、年龄、性别)以及他们爬过的山(姓名、高度) 我已经创建了登山者课程和登山课程。我还为登山者创建了一个ArrayList,可以添加到这个数组中。我的问题是。。我怎样才能将一个登山者添加到登山者列表中,并能够同时添加他们攀登过的山及其高度 添加攀岩者的方法需要同时访问“攀岩者”和“Mountain”类?我需要把山地班的场地传给登山运动员吗 虽然代码解决的问题是赞赏的,如果我是在正确的方向,使我可以了解更多的帮助 谢谢Java 创建合并两个对象字段的ArrayList?,java,arraylist,bluej,Java,Arraylist,Bluej,我是Java新手,正在使用BlueJ 课程背景概述 我正在尝试创建一个俱乐部数据库(arraylist,而不是实际的数据库),用户可以在其中向arraylist添加一个新的登山者(姓名、年龄、性别)以及他们爬过的山(姓名、高度) 我已经创建了登山者课程和登山课程。我还为登山者创建了一个ArrayList,可以添加到这个数组中。我的问题是。。我怎样才能将一个登山者添加到登山者列表中,并能够同时添加他们攀登过的山及其高度 添加攀岩者的方法需要同时访问“攀岩者”和“Mountain”类?我需要把山地班
import java.util.ArrayList;
import java.util.Scanner;
/**
* Write a description of class ClubStats here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ClubStats
{
// An ArrayList for storing climber details.
private ArrayList<Climber> climbers;
/**
* Constructor for objects of class ClubStats
*/
public ClubStats()
{
// Initialise instance variables.
climbers = new ArrayList<Climber>();
}
public void addClimber(Climber newName)
{
climbers.add(newName);
}
public Climber getClimber(String name)
{
Climber foundClimber = null;
int index = 0;
boolean searching = true;
while(searching && index < climbers.size()) {
Climber climber = climbers.get(index);
if(climber.getName().equals(name)) {
searching = false;
foundClimber = climber;
}
else {
System.out.println(name + " not found");
index++;
}
}
return foundClimber;
}
public void displayList()
{
for (int item = 0; item<climbers.size();
item++) {
Climber climber = climbers.get(item);
System.out.println(climber.getName() + (" ") + climber.getAge() + (" ")
+ climber.getGender());
}
}
}
public class Climber
{
// Instance variables.
// The climber name.
private String name;
// The climber age
private int age;
// The climber gender.
private String gender;
/**
* Constructor for objects of class Climber
*/
public Climber (String newName, int newAge, String newGender)
{
// Initialise instance variables.
name = newName;
age = newAge;
gender = newGender;
}
/**
* Accessor method for climber's name.
*/
public String getName()
{
return name;
}
/**
* Set the climber's name.
*/
public void setName(String newName)
{
name = newName;
}
/**
* Accessor method for climber's age.
*/
public int getAge()
{
return age;
}
/**
* Set the climber's age.
*/
public void setAge(int newAge)
{
age = newAge;
}
/**
* Set the climer's gender.
*/
public String getGender()
{
return gender;
}
/**
* Accessor method for climber's gender.
*/
public void getGender(String newGender)
{
gender = newGender;
}
}
public class Mountain
{
// Instance variables.
private double height;
private String name;
/**
* Constructor for objects of class Mountain
*/
public Mountain(String mName, double mHeight)
{
// Initialise instance variables
name = mName;
height = mHeight;
}
/**
* Accessor method for mountain name.
*/
public String getName()
{
return name;
}
/**
* Set the mountain name.
*/
public void setName(String newName)
{
name = newName;
}
/**
* Accessor method for mountain height.
*/
public double getHeight()
{
// put your code here
return height;
}
/**
* Set the mountain height.
*/
public void setHeight(double newHeight)
{
height = newHeight;
}
}
import java.util.ArrayList;
导入java.util.Scanner;
/**
*在这里写一篇关于类ClubStats的描述。
*
*@author(你的名字)
*@version(版本号或日期)
*/
公共类俱乐部
{
//用于存储登山者详细信息的ArrayList。
私人攀岩者;
/**
*ClubStats类对象的构造函数
*/
公共俱乐部
{
//初始化实例变量。
攀登者=新阵列列表();
}
公共攀岩者(攀岩者新名称)
{
攀岩者。添加(新名称);
}
公共爬山器GetClimper(字符串名称)
{
爬升器发现爬升器=空;
int指数=0;
布尔搜索=真;
while(搜索和索引 对于(int item=0;itemA攀登者
可以包含该攀登者攀登的山脉
的列表
您可以将addMountain(Mountain Mountain)
方法添加到climper
类中,该类将Mountain
添加到列表中
您可以添加一个getter方法List getMountainList()
,该方法将返回List
这样,对于每个攀岩者
您都可以访问山
s该攀岩者
已经攀岩
请注意,如果多个攀登者
攀登了同一座山
,他们都可以在他们的列表
中包含对同一座山
实例的引用(即无需为每个攀登者
创建该山
实例的副本).你是在创建一个多维数组来存储登山者和山脉吗?也许我误解了你的意图,但我不明白你为什么要使用数组列表来存储类似的内容。为什么不创建一个“山脉”呢攀岩者类中可以记录攀登的山脉的字段?它甚至可以是arrayList字段,在这种情况下,您可以将他们攀登的所有山脉添加到此列表中,并将其绑定到每个攀岩者。