【问题标题】:Constructor and Array Lists [duplicate]构造函数和数组列表 [重复]
【发布时间】:2019-05-03 04:46:25
【问题描述】:

我正在尝试创建一个(美国总统的)测验程序。我想创建一个数组列表(总统编号、总统姓名、起始年份、去年)。

由于某种原因,我的构造函数不起作用,我不得不构建另一个方法,但第二种方法导致 StackOverFlowError (这是我第一次使用这个网站来解决问题,我想我来到了给定站点名称的正确位置)

这是我的代码:

public class Presidents 
{   

    public int presidentNumber;
    public String presidentName;
    public int startingYear;
    public int endingYear;

    public Presidents(int PresidentNumber, String NameOfPresident, int StartingYear, int EndingYear)
    {
        this.presidentNumber = PresidentNumber;
        this.presidentName = NameOfPresident;
        this.startingYear = StartingYear;
        this.endingYear = EndingYear;

    }

    public Presidents() {

    }

    public Presidents [] PresidentList () 
    {           
        Presidents [] president = new Presidents [45];
        president [0] = Presidents(1, "George Washington", 1789, 1797);
        president [1] = Presidents(2, "John Adams", 1797, 1801);
        president [2] = Presidents(3, "Thomas Jefferson", 1801, 1809);
        president [3] = Presidents(4, "James Madison", 1809, 1817);
        president [4] = Presidents(5, "James Monroe", 1817, 1825);
        president [5] = Presidents(6, "John Quincy Adams", 1825, 1829);
        president [6] = Presidents(7, "Andrew Jackson", 1829, 1837);
        president [7] = Presidents(8, "Martin Van Buren", 1837, 1841);
        president [8] = Presidents(9, "William Henry Harrison", 1841, 1841);
        president [9] = Presidents(10, "John Tyler", 1841, 1845);
        president [10] = Presidents(11, "James K. Polk", 1845, 1849);
        president [11] = Presidents(12, "Zachary Taylor", 1849, 1850);
        president [12] = Presidents(13, "Millard Fillmore", 1850, 1853);
        president [13] = Presidents(14, "Franklin Pierce", 1853, 1857);
        president [14] = Presidents(15, "James Buchanan", 1857, 1861);
        president [15] = Presidents(16, "Abraham Lincoln", 1861, 1865);
        president [16] = Presidents(17, "Andrew Johnson", 1865, 1869);
        president [17] = Presidents(18, "Ulysses S. Grant", 1869, 1877);
        president [18] = Presidents(19, "Rutherford B. Hayes", 1877, 1881);
        president [19] = Presidents(20, "James A. Garfield", 1881, 1881);
        president [20] = Presidents(21, "Chester A. Arthur", 1881, 1885);
        president [21] = Presidents(22, "Grover Cleveland", 1885, 1889);
        president [22] = Presidents(23, "Benjamin Harrison", 1889, 1893);
        president [23] = Presidents(24, "Grover Cleveland", 1893, 1897);
        president [24] = Presidents(25, "William McKinley", 1897, 1901);
        president [25] = Presidents(26, "Theodore Roosevelt", 1901, 1909);
        president [26] = Presidents(27, "William Howard Taft", 1909, 1913);
        president [27] = Presidents(28, "Woodrow Wilson", 1913, 1921);
        president [28] = Presidents(29, "Warren G. Harding", 1921, 1923);
        president [29] = Presidents(30, "Calvin Coolidge", 1923, 1929);
        president [30] = Presidents(31, "Herbert Hoover", 1929, 1933);
        president [31] = Presidents(32, "Franklin D. Roosevelt", 1933, 1945);
        president [32] = Presidents(33, "Harry S. Truman", 1945, 1953);
        president [33] = Presidents(34, "Dwight D. Eisenhower", 1953, 1961);
        president [34] = Presidents(35, "John F. Kennedy", 1961, 1963);
        president [35] = Presidents(36, "Lyndon B. Johnson", 1963, 1969);
        president [36] = Presidents(37, "Richard Nixon", 1969, 1974);
        president [37] = Presidents(38, "Gerald Ford", 1974, 1977);
        president [38] = Presidents(39, "Jimmy Carter", 1977, 1981);
        president [39] = Presidents(40, "Ronald Reagan", 1981, 1989);
        president [40] = Presidents(41, "George H. W. Bush", 1989, 1993);
        president [41] = Presidents(42, "Bill Clinton", 1993, 2001);
        president [42] = Presidents(43, "George W. Bush", 2001, 2009);
        president [43] = Presidents(44, "Barack Obama", 2009, 2017);
        president [44] = Presidents(45, "Donald Trump", 2017, 2018);

        return president;
    }   

    private Presidents Presidents(int i, String string, int j, int k) {         
        return Presidents (i, string, j, k);
    }

}
import java.util.Scanner;
import java.util.Random;


public class PresidentsQuiz {

    public static void main (String [] args)    
    {       
        System.out.println("Do you know the 45 presidents of the United States? Enter a number between 1 and 45");

        Scanner kb = new Scanner (System.in);           
        int input =  kb.nextInt();

        while (input < 1 ||  input > 45)        
        {
            System.out.println("Enter a number between 1 and 45");
            input =  kb.nextInt();
        }

        Presidents president = new Presidents ();                   
        Presidents [] presidentList = president.PresidentList();                        
        System.out.println(presidentList [0]);
        kb.close();

    }

}

【问题讨论】:

  • 作为旁注,我建议查看 Java 命名约定 here。遵循这些约定将有助于使您的代码更具可读性。

标签: java arrays list constructor


【解决方案1】:

你这里有一个方法:

public Presidents [] PresidentList ()

    {       


        Presidents [] president = new Presidents [45];
        president [0] = Presidents(1, "George Washington", 1789, 1797);
        president [1] = Presidents(2, "John Adams", 1797, 1801);
        president [2] = Presidents(3, "Thomas Jefferson", 1801, 1809);
        president [3] = Presidents(4, "James Madison", 1809, 1817);
        president [4] = Presidents(5, "James Monroe", 1817, 1825);
        //...
        return president;
}

但是每次调用:

president [...] = Presidents(...);

将调用:

private Presidents Presidents(int i, String string, int j, int k) {

    return Presidents (i, string, j, k);
}

调用相同参数的相同方法,将永远递归,导致堆栈溢出错误。


没有一个方法与类调用相同的东西,并返回一些东西。这是为构造函数保留的。你已经有一个构造函数接受正确的参数,所以只需删除该方法:

private Presidents Presidents(int i, String string, int j, int k) {

    return Presidents (i, string, j, k);
}

然后在你的方法中你需要添加new关键字:

president [0] = new Presidents(1, "George Washington", 1789, 1797);
                 ^--- Here

【讨论】:

  • 当我尝试使用构造函数时出现错误。它说“Presidents(int, String, int, int) 方法未定义总统类型”,它迫使我创建那个方法(你告诉我删除的那个)。
【解决方案2】:

您应该在初始化数组时创建 Presidents 类的新对象,因为数组应该存储 Presidents 类型的对象。相反,您只调用了类的构造函数。而不是这样做:

president [0] = Presidents(1, "George Washington", 1789, 1797);

你应该这样做:

president [0] = new Presidents(1, "George Washington", 1789, 1797);

对其余的数组索引也这样做。而这个方法:

private Presidents Presidents(int i, String string, int j, int k) {         
    return Presidents (i, string, j, k);
}

这是定义方法的错误方式。该方法不应与类同名。只有构造函数应该拥有它。此外,如果您希望此方法返回 Presidents 类的对象,则返回语句应如下所示:

return Presidents (i, string, j, k);

但是这个方法是没有意义的,因为你已经有 PresidentList() 方法返回数组。话虽如此,最好将类中的变量设为私有并定义 getter 方法来访问它们。无论如何,我已经对您的代码进行了一些修改,这应该可以工作:

public class Presidents 
{   

public int presidentNumber;
public String presidentName;
public int startingYear;
public int endingYear;
Presidents [] president;

public Presidents(int PresidentNumber, String NameOfPresident, int StartingYear, int EndingYear)
{
    this.presidentNumber = PresidentNumber;
    this.presidentName = NameOfPresident;
    this.startingYear = StartingYear;
    this.endingYear = EndingYear;

}

public Presidents() {

}

public Presidents [] PresidentList () 
{           
     president= new Presidents [45];
    president [0] = new Presidents(1, "George Washington", 1789, 1797);
    president [1] = new Presidents(2, "John Adams", 1797, 1801);
    president [2] = new Presidents(3, "Thomas Jefferson", 1801, 1809);
    president [3] = new Presidents(4, "James Madison", 1809, 1817);
    president [4] = new Presidents(5, "James Monroe", 1817, 1825);
    president [5] = new Presidents(6, "John Quincy Adams", 1825, 1829);
    president [6] = new Presidents(7, "Andrew Jackson", 1829, 1837);
    president [7] = new Presidents(8, "Martin Van Buren", 1837, 1841);
    president [8] = new Presidents(9, "William Henry Harrison", 1841, 1841);
    president [9] = new Presidents(10, "John Tyler", 1841, 1845);
    president [10] = new Presidents(11, "James K. Polk", 1845, 1849);
    president [11] = new Presidents(12, "Zachary Taylor", 1849, 1850);
    president [12] = new Presidents(13, "Millard Fillmore", 1850, 1853);
    president [13] = new Presidents(14, "Franklin Pierce", 1853, 1857);
    president [14] = new Presidents(15, "James Buchanan", 1857, 1861);
    president [15] = new Presidents(16, "Abraham Lincoln", 1861, 1865);
    president [16] = new Presidents(17, "Andrew Johnson", 1865, 1869);
    president [17] = new Presidents(18, "Ulysses S. Grant", 1869, 1877);
    president [18] =new Presidents(19, "Rutherford B. Hayes", 1877, 1881);
    president [19] =new Presidents(20, "James A. Garfield", 1881, 1881);
    president [20] =new Presidents(21, "Chester A. Arthur", 1881, 1885);
    president [21] =new Presidents(22, "Grover Cleveland", 1885, 1889);
    president [22] =new Presidents(23, "Benjamin Harrison", 1889, 1893);
    president [23] =new Presidents(24, "Grover Cleveland", 1893, 1897);
    president [24] =new Presidents(25, "William McKinley", 1897, 1901);
    president [25] =new Presidents(26, "Theodore Roosevelt", 1901, 1909);
    president [26] =new Presidents(27, "William Howard Taft", 1909, 1913);
    president [27] =new Presidents(28, "Woodrow Wilson", 1913, 1921);
    president [28] =new Presidents(29, "Warren G. Harding", 1921, 1923);
    president [29] =new Presidents(30, "Calvin Coolidge", 1923, 1929);
    president [30] =new Presidents(31, "Herbert Hoover", 1929, 1933);
    president [31] =new Presidents(32, "Franklin D. Roosevelt", 1933, 1945);
    president [32] =new Presidents(33, "Harry S. Truman", 1945, 1953);
    president [33] =new Presidents(34, "Dwight D. Eisenhower", 1953, 1961);
    president [34] =new Presidents(35, "John F. Kennedy", 1961, 1963);
    president [35] =new Presidents(36, "Lyndon B. Johnson", 1963, 1969);
    president [36] =new Presidents(37, "Richard Nixon", 1969, 1974);
    president [37] =new Presidents(38, "Gerald Ford", 1974, 1977);
    president [38] =new Presidents(39, "Jimmy Carter", 1977, 1981);
    president [39] =new Presidents(40, "Ronald Reagan", 1981, 1989);
    president [40] =new Presidents(41, "George H. W. Bush", 1989, 1993);
    president [41] =new Presidents(42, "Bill Clinton", 1993, 2001);
    president [42] =new Presidents(43, "George W. Bush", 2001, 2009);
    president [43] =new Presidents(44, "Barack Obama", 2009, 2017);
    president [44] =new Presidents(45, "Donald Trump", 2017, 2018);

    return president;
}   

}

还有主课:

import java.util.Scanner;
import java.util.Random;


public class PresidentsQuiz {

public static void main (String [] args)    
{       
    System.out.println("Do you know the 45 presidents of the United States? Enter a number between 1 and 45");

    Scanner kb = new Scanner (System.in);           
    int input =  kb.nextInt();

    while (input < 1 ||  input > 45)        
    {
        System.out.println("Enter a number between 1 and 45");
        input =  kb.nextInt();
    }

    Presidents president = new Presidents ();                   
    Presidents [] presidentList = president.PresidentList();                        
    //System.out.println(presidentList [0].presidentName);

    for(int i=0;i<input;i++)
    {
        Presidents p = presidentList[i];
        System.out.println(p.presidentNumber+", "+p.presidentName+", "+p.startingYear+", "+p.endingYear);
    }

    kb.close();

}

}

我认为您试图打印所有总统,直到您输入的数字。如果没有,您可以删除 main 方法中的 for 循环。

【讨论】:

    【解决方案3】:

    解决方案

    • 必须将创建总统列表的逻辑移至主类

    原因

    • 逻辑是程序的职责
    • 为各个类分配职责至关重要
    • 一个对象可以负责使用构造函数创建它自己的单个实例。但不负责使用其他实例方法创建自己的实例列表。
    • 从您的代码中,您将拥有一个字段中没有任何值的总裁对象。该虚拟实例负责创建真实的总统列表。

    代码

    public class Presidents {   
    
    public int presidentNumber;
    public String presidentName;
    public int startingYear;
    public int endingYear;
    
    public Presidents(int PresidentNumber, String NameOfPresident, int StartingYear, int EndingYear)
    {
        this.presidentNumber = PresidentNumber;
        this.presidentName = NameOfPresident;
        this.startingYear = StartingYear;
        this.endingYear = EndingYear;
    
    }
    public Presidents() {}
    }
    
    import java.util.Scanner;
    import java.util.Random;
    
    
    public class PresidentsQuiz {
    
    public static void main (String [] args)    
    {       
        System.out.println("Do you know the 45 presidents of the United States? Enter a number between 1 and 45");
    
        Scanner kb = new Scanner (System.in);           
        int input =  kb.nextInt();
    
        while (input < 1 ||  input > 45)        
        {
            System.out.println("Enter a number between 1 and 45");
            input =  kb.nextInt();
        }
    
        Presidents [] president = new Presidents [45];
        president [0] = new Presidents(1, "George Washington", 1789, 1797);
        president [1] = new Presidents(2, "John Adams", 1797, 1801);
        president [2] = new Presidents(3, "Thomas Jefferson", 1801, 1809);
        president [3] = new Presidents(4, "James Madison", 1809, 1817);
        president [4] = new Presidents(5, "James Monroe", 1817, 1825);
        president [5] = new Presidents(6, "John Quincy Adams", 1825, 1829);
        president [6] = new Presidents(7, "Andrew Jackson", 1829, 1837);
        president [7] = new Presidents(8, "Martin Van Buren", 1837, 1841);
        president [8] = new Presidents(9, "William Henry Harrison", 1841, 1841);
        president [9] = new Presidents(10, "John Tyler", 1841, 1845);
        president [10] = new Presidents(11, "James K. Polk", 1845, 1849);
        president [11] = new Presidents(12, "Zachary Taylor", 1849, 1850);
        president [12] = new Presidents(13, "Millard Fillmore", 1850, 1853);
        president [13] = new Presidents(14, "Franklin Pierce", 1853, 1857);
        president [14] = new Presidents(15, "James Buchanan", 1857, 1861);
        president [15] = new Presidents(16, "Abraham Lincoln", 1861, 1865);
        president [16] = new Presidents(17, "Andrew Johnson", 1865, 1869);
        president [17] = new Presidents(18, "Ulysses S. Grant", 1869, 1877);
        president [18] = new Presidents(19, "Rutherford B. Hayes", 1877, 1881);
        president [19] = new Presidents(20, "James A. Garfield", 1881, 1881);
        president [20] = new Presidents(21, "Chester A. Arthur", 1881, 1885);
        president [21] = new Presidents(22, "Grover Cleveland", 1885, 1889);
        president [22] = new Presidents(23, "Benjamin Harrison", 1889, 1893);
        president [23] = new Presidents(24, "Grover Cleveland", 1893, 1897);
        president [24] = new Presidents(25, "William McKinley", 1897, 1901);
        president [25] = new Presidents(26, "Theodore Roosevelt", 1901, 1909);
        president [26] = new Presidents(27, "William Howard Taft", 1909, 1913);
        president [27] = new Presidents(28, "Woodrow Wilson", 1913, 1921);
        president [28] = new Presidents(29, "Warren G. Harding", 1921, 1923);
        president [29] = new Presidents(30, "Calvin Coolidge", 1923, 1929);
        president [30] = new Presidents(31, "Herbert Hoover", 1929, 1933);
        president [31] = new Presidents(32, "Franklin D. Roosevelt", 1933, 1945);
        president [32] = new Presidents(33, "Harry S. Truman", 1945, 1953);
        president [33] = new Presidents(34, "Dwight D. Eisenhower", 1953, 1961);
        president [34] = new Presidents(35, "John F. Kennedy", 1961, 1963);
        president [35] = new Presidents(36, "Lyndon B. Johnson", 1963, 1969);
        president [36] = new Presidents(37, "Richard Nixon", 1969, 1974);
        president [37] = new Presidents(38, "Gerald Ford", 1974, 1977);
        president [38] = new Presidents(39, "Jimmy Carter", 1977, 1981);
        president [39] = new Presidents(40, "Ronald Reagan", 1981, 1989);
        president [40] = new Presidents(41, "George H. W. Bush", 1989, 1993);
        president [41] = new Presidents(42, "Bill Clinton", 1993, 2001);
        president [42] = new Presidents(43, "George W. Bush", 2001, 2009);
        president [43] = new Presidents(44, "Barack Obama", 2009, 2017);
        president [44] = new Presidents(45, "Donald Trump", 2017, 2018);
    
        System.out.println(presidentList [0]);
        kb.close();
    
    }
    
    }
    

    【讨论】:

      【解决方案4】:

      当您在列表中的特定位置进行分配时,您需要实例化一个新的总裁对象。

      这里你的方法:-

      private Presidents Presidents(int i, String string, int j, int k) {
              //does not Instantiate a new object
              return Presidents (i, string, j, k);
          }
      

      -这里它不实例化Presidents 类的新对象。它调用相同的总统方法并发生递归调用。

      所以这里你需要用new 关键字来实例化你的方法。

          return new Presidents (i, string, j, k);
      

      它指向类,构造函数将被调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        • 2013-10-13
        • 1970-01-01
        • 1970-01-01
        • 2018-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多