英文:
LinkedList with years
问题
我必须编写一个带有两个位置的链表。第一个位置是名称,第二个位置是年份。
class Person
{
String name;
int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
}
class LinkedListStart {
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Person> persons = new LinkedList<Person>();
persons.add(new Person("Joe", 2010));
persons.add(new Person("Jane", 2012));
persons.add(new Person("Charly", 1910));
persons.add(new Person("Daisy", 1908));
System.out.println("LinkedList : " + persons);
}
}
在运行代码时出现了错误:
LinkedList : [Person@6d06d69c, Person@7852e922, Person@4e25154f, Person@70dea4e]
是否有可能做到这一点?我想使用 for-each 循环查找1950年之前的人。
如何编写类似这样的代码?
<details>
<summary>英文:</summary>
I have to code LinkedList with two positions. First is the name and the second is the year.
class Person
{
String name;
int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
}
class LinkedListStart {
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Person> persons = new LinkedList<Person>();
persons.add(new Person("Joe", 2010));
persons.add(new Person("Jane", 2012));
persons.add(new Person("Charly", 1910));
persons.add(new Person("Daisy", 1908));
System.out.println("LinkedList : " + persons);
}
}
There are errors when I am running the code:
LinkedList : [Person@6d06d69c, Person@7852e922, Person@4e25154f, Person@70dea4e]
Is it possible to do it? I want do use for-each loop to find person who is below 1950.
How to code something like this?
</details>
# 答案1
**得分**: 1
你应该按以下方式定义一个名为 Person 的类:
```java
class Person{
String name;
int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
}
然后按以下方式创建一个 LinkedList
:
LinkedList<Person> persons = new LinkedList<>();
英文:
You should define a class Person as follows:
class Person{
String name;
int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
}
And then create a LinkedList
as follows:
LinkedList<Person> persons = new LinkedList<>();
答案2
得分: 1
你可以将它们合并成一个String
,例如 names.add("Joe 2010")
(不是推荐的做法,也不是特别好的方法),或者创建一个Person
类来包含这两个属性,但是你不能在一个位置上添加两个元素,就像你在上面的代码中尝试做的那样。
如果你选择使用Person
类的方式,遍历每个元素,并检查Person.year
或者你选择的属性是否小于1950。
如果你选择使用String
的方式,遍历每个元素,在空格上拆分String
以创建一个数组,其中第一个元素是姓名,第二个元素是年份,使用Integer.parseInt
从年份String
解析出一个int
,然后检查年份是否小于1950。正如@Michael提到的,不推荐像这样组合两个字符串。
编辑:关于你的新问题,Java的LinkedList<T>
类是一个泛型类,因为它接受一个类型参数,比如在LinkedList<Person>
中的Person
部分。因为你的类没有被定义为泛型类,并且与内置的LinkedList
具有相同的名称,所以当你尝试传入类型参数时会出现此错误,因为Java认为你在尝试使用你自己的LinkedList
类。一般来说,即使你在代码中没有使用特定的内置类,最好也不要使用与内置类相同的名称来命名你自己的类,以防止发生这种冲突。只需重命名你自己的类,你的代码就会正常工作。
英文:
You could combine them both into one String
like names.add("Joe 2010")
(NOT a recommended, or particularly good approach) or create a Person
class to include both of these attributes, but you can't add two elements in the space of one, like you are attempting to do with your code above.
If you go with the Person
class route, iterate through each element, and check if Person.year
or whatever you choose to call the attribute is less than 1950.
If you go with the String
route, iterate through each, split the String
on a space to create an array, where the first element is the name and the second is the year, parse an int
out of the year String
using Integer.parseInt
and then check if the year is less than 1950. As @Michael mentioned, combining two strings like this is not recommended
Edit: As for your new problem, Java's LinkedList<T>
class is known as a generic class, because it takes a type parameter, such as the Person
part in LinkedList<Person>
. Because your class is not defined as a generic class, and has the same name as the built-in LinkedList
, you are receiving this error when you try to pass in a type parameter, as Java thinks you are trying to use your own LinkedList
class. In general, even if you are not using a particular built-in class in your code, it's best not to name your own classes with the same name as built-ins, so collisions like this don't happen. Simply rename your own class, and your code will work
答案3
得分: 0
是的,您可以创建一个自定义列表,以下是代码:
public class HomeDataModel {
private String name;
private int year;
public HomeDataModel() {
}
public HomeDataModel(String name, int year) {
this.name = name;
this.year = year;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
// 在您想要使用列表的活动中
List<HomeDataModel> listname = new ArrayList<>();
listname.add(new HomeDataModel("yourname", Youryear));
for (int i = 0; i < listname.size(); i++) {
int check = listname.get(i).getYear();
if (check < 1950) {
String newname = listname.get(i).getName();
Toast.makeText(MapsActivity.this, newname, Toast.LENGTH_SHORT).show();
}
}
请确保在使用类的构造函数中正确地设置成员变量值。另外,请注意列表名称和变量名的一致性,以及传递给 add
函数的参数。
英文:
Yes you can create a custom list here is the code,
create a class name it what ever you want, i named it HomeDataModel
public class HomeDataModel {
private String name;
private int year;
public HomeDataModel() {
}
public HomeDataModel(String name,int year) {
name=name;
year= year;
}
public String getName() {
return name;
}
public void setName(String name) {
name= name;
}
public String getYear() {
return year;
}
public void setYear(int year) {
year= year;
}
}
now in your activity where you want to use list,,
//creating a custom list
List<HomeDataModel> listname = new ArrayList<>();
list.add(new HomeDataModel("yourname", "Youryear"));
//code for check how is below 1950
for(int i=0; i<listname.size(); i++){
int check = list.get(i).getYear();
if(check <1950){
String newname = list.get(i).getName(); //the name is stored
Toast.makeText(MapsActivity.this,newname, Toast.LENGTH_SHORT).show();
}
}
答案4
得分: 0
在运行代码时出现错误:LinkedList: [Person@6d06d69c,Person@7852e922,Person@4e25154f,Person@70dea4e]
Java中的每个类默认都有toString()方法,如果将该类的某个对象传递给System.out.println(),则会调用该方法。默认情况下,此调用返回该对象的className@hashcode。在这里,您没有覆盖toString方法,这就是为什么默认情况下会打印出那样的结果。请使用以下代码:
public class Test {
static class Person {
String name;
int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
@Override
public String toString() {
return "Person [name=" + name + ", year=" + year + "]";
}
}
public static void main(String[] args) throws java.lang.Exception {
LinkedList<Person> persons = new LinkedList<Person>();
persons.add(new Person("Joe", 2010));
persons.add(new Person("Jane", 2012));
persons.add(new Person("Charly", 1910));
persons.add(new Person("Daisy", 1908));
for (Person person : persons) {
System.out.println("Name is::" + person.name + " year is:::" + person.year);
}
// 用于打印人员信息,重写了toString方法
System.out.println(persons);
}
}
英文:
> There are errors when I am running the code: LinkedList : [Person@6d06d69c, Person@7852e922, Person@4e25154f, Person@70dea4e]
Every class in Java has the toString() method in it by default, which is called if you pass some object of that class to System.out.println(). By default, this call returns the className@hashcode of that object.
Here you have not overridden the toString method that's why it is printing like that by default.Please us below code:
public class Test {
static class Person
{
String name;
int year;
public Person(String name, int year) {
this.name = name;
this.year = year;
}
@Override
public String toString() {
return "Person [name=" + name + ", year=" + year + "]";
}
}
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Person> persons = new LinkedList<Person>();
persons.add(new Person("Joe", 2010));
persons.add(new Person("Jane", 2012));
persons.add(new Person("Charly", 1910));
persons.add(new Person("Daisy", 1908));
for (Person person : persons) {
System.out.println("Name is::"+person.name+" year is:::"+person.year);
}
//for printing the person override tostring method
System.out.println(persons);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论