繁体   English   中英

需要Array(list?),但是int found错误

[英]Array (list?) required, but int found error

我有以下代码:

import java.util.ArrayList;
public class TestCandidate2
{
public static void main(String[] args) {
 ArrayList<Candidate> election = new ArrayList<Candidate>();

 Candidate john = new Candidate("John Smith", 5000);
 election.add(john);
 Candidate mary = new Candidate("Mary Miller", 4000);
 election.add(mary);
 Candidate michael = new Candidate("Micheal Duffy", 6000);
 election.add(michael);
 Candidate tim = new Candidate("Tim Robinson", 2500);
 election.add(tim);
 Candidate joe = new Candidate("Joe Ashtony", 1800);
 election.add(joe);

 System.out.println("Results Per Candidate:");
 System.out.println("________________________");
 System.out.print("\n");
 int totalVotes = 0;
 int total = 0;

 for(Candidate dec : election) {
    System.out.println(dec.toString());
    total += dec.getVotes();
    totalVotes += dec.getVotes();
 }

 System.out.print("\n");
 System.out.println("Total number of votes in election: " + totalVotes);
}

public static void printVotes(ArrayList<Candidate> table) {
    for(int i = 0; i < table.size(); i++) {
       System.out.println(table.size()[i]);
    }
}

/**public static int getTotal(ArrayList<Candidate> table)  {
    int total = 0;
    for(int i = 0; i < table.size(); i++) {
       total = table[i].getVotes() + total;
    }
    return total;
}*/

/**public static void printResults(ArrayList<Candidate> table)  {
    double total = getTotal(table);
    System.out.print("Candidate               Votes Received              % of Total Votes");
    System.out.print("\n");
    for(int i = 0; i < table.length; i++) {
       System.out.printf("%s %17d %25.0f", table[i].getName(), table[i].getVotes(), ((table[i].getVotes() / total) * 100));
       System.out.println("");
    }
}*/
}

现在首先关闭我认为我应该得到的错误是'arraylist required,但是int found',但是我得到的错误是'数组需要,但是找到了int'。

我不知道我应该如何修复int i,因为无论什么时候我把[]声明为数组,我仍然会得到错误。 我尝试过研究,但似乎没有人有我的确切问题。

您似乎错误地访问了List 代替:

table[i].getVotes()

尝试:

table.get(i).getVotes();

或者完全避免使用索引。 例如,您可以将getTotal()重写为:

public static int getTotal(List<Candidate> candidates) {
    int total = 0;
    foreach (Candidate c : candidates) {
        total += c.getVotes();
    }
    return total;
} 

产生错误的行:

System.out.println(table.size()[i]);

有点令人困惑,但我想你想要:

System.out.println(table.get(i).getVotes());

您可以使用List.get(int)按位置获取元素(不是数组的[] )。 另外,我建议你编程到列表界面。 喜欢

public static void printVotes(List<Candidate> table) {
    for(int i = 0; i < table.size(); i++) {
       System.out.println(table.get(i));
    }
}

你也可以使用for-each循环+= like,

public static int getTotal(List<Candidate> table)  {
    int total = 0;
    for(Candidate c : table) {
       total += c.getVotes();
    }
    return total;
}

你应该使用table.get(i)而不是table[i] 这是一个ArrayList而不是一个数组

需要数组,但要列出<object>成立<div id="text_translate"><p>我正在尝试通过 for 循环以及我创建的 int random 将我创建的新列表添加到 go 以随机化我在 object 列表中的项目。 <em><strong>bank[i]</strong></em>和<em><strong>this.bank[random]</strong></em>给了我错误“需要数组,但找到了列表”。 我不确定如何使用 object 列表执行此操作。 <strong>编辑:我已经修复了我的主要错误,但现在我的随机化 function 在我设置要使用的问题数量时超出了界限</strong></p><h1>银行代码</h1><pre>import java.io.*; import java.util.*; public class TestBank { public List&lt;Object&gt; bank; public TestBank() { // constructor this.bank = bank(); } public static List&lt;Object&gt; bank() { List&lt;Object&gt; testBank = new ArrayList&lt;&gt;(); String mcOne [] = {"Spongebob", "Fiore", "Patrick", "Sandy Cheeks"}; String mcTwo [] = {"Canada", "China", "Russia", "United States"}; String mcThree [] = {"California", "Pennsylvania", "Texas", "Alaska"}; String mcFour [] = {"Washington, D.C.", "Boston", "Manhattan", "Philadelphia"}; String mcFive [] = {"200", "206", "201", "205"}; String mcSix [] = {"Shakespeare", "Stephen King", "Nathanial Hawthorne", "Ernest Hemingway"}; String mcSeven [] = {"Jupiter", "Saturn", "Neptune", "Earth"}; String mcEight [] = {"Van Gogh", "Da Vinci", "Picasso", "Monet"}; testBank.add(new ObjectiveQuestion(5, 1, 1, "How many points is a touchdown in American Football worth?", "6 points")); testBank.add(new ObjectiveQuestion(5, 2, 1, "Which US state is known for peaches?", "Georgia")); testBank.add(new ObjectiveQuestion(5, 5, 1, "Name the longest river in the uk.", "River Severn")); testBank.add(new FillInTheBlankQuestion(5, 2, 1, "Temple Universities Mascot is __.", "An Owl", 6)); testBank.add(new FillInTheBlankQuestion(20, 3, 1, "__ was the 16th US President.", "Abraham Lincoln", 15)); testBank.add(new MultipleChoiceQuestion(5, 1, 1, "Who lives in a pineapple under the sea?", "Spongebob", mcOne)); testBank.add(new MultipleChoiceQuestion(5, 4, 1, "What is the largest country in the world?", "Russia", mcTwo)); testBank.add(new MultipleChoiceQuestion(5, 4, 1, "What state is the largest state of the United States of America?", "Alaska", mcThree)); testBank.add(new MultipleChoiceQuestion(5, 2, 1, "In which city can you find the Liberty Bell?", "Philadelphia", mcFour)); testBank.add(new MultipleChoiceQuestion(5, 3, 1, "How many bones are there in the human body?", "206", mcFive)); testBank.add(new MultipleChoiceQuestion(5, 3, 1, "Who wrote The Scarlett Letter?", "Nathanial Hawthorne", mcSix)); testBank.add(new MultipleChoiceQuestion(5, 1, 1, "Which planet in our solar system is the largest?", "Jupiter", mcSeven)); testBank.add(new MultipleChoiceQuestion(5, 2, 1, "Who painted the Mona Lisa?", "Da Vinci", mcEight)); testBank.add(new FillInTheBlankQuestion(5, 1, 1, "The country that consumes the most chocolate is __", "Switzerland", 11)); testBank.add(new FillInTheBlankQuestion(5, 2, 1, "__ is also known as Sodium Chloride", "Salt", 4)); testBank.add(new FillInTheBlankQuestion(5, 2, 1, "__ is the Olympic sport that Michael Phelps is known for.", "Swimming", 8)); testBank.add(new ObjectiveQuestion(5, 2, 1, "Which astrological sign is a crab?", "Cancer")); testBank.add(new ObjectiveQuestion(5, 4, 1, "How many boroughs are there in New York City?", "5")); testBank.add(new ObjectiveQuestion(5, 5, 1, "What is the longest river in the world?", "Nile")); testBank.add(new ObjectiveQuestion(5, 1, 1, "How many days are in February during a leap year?", "29")); //Scanner in = new Scanner(System.in); //newTest(in); //readFile(in); return testBank; } public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(System.in); TestBank test = new TestBank(); test.newTest(in); } public void writeFile(String a, String b) throws FileNotFoundException { PrintStream bankFile = new PrintStream(new File(a + ".txt")); bankFile.print(b); } // makes new test files public boolean newTest(Scanner in) throws FileNotFoundException { try{ // for NullPointerException error int amountQuestions = this.bank.size(); // amount of questions in bank //System.out.println(amountQuestions); int size = findLength(this.bank, in); // int for finding the length of questions to use //System.out.println(size); List&lt;Object&gt; bank = randomize(amountQuestions, size); // calls randomize function for size set Test newTest = new Test(bank); //String test = "-=-=-=-=-=-= [Questions] [Pts. " + newTest.getPoints() + "] =-=-=-=-=-=-\n\n" + newTest.toString(); //String answers = "-=-=-=-=-=-= [Answers] [Pts. " + newTest.getPoints() + "] =-=-=-=-=-=-\n\n" + newTest.toAnswerString(); String combined = "-=-=-=-=-=-= [Questions] [Pts. " + newTest.getPoints() + "] =-=-=-=-=-=-\n\n" + newTest.toString() + "-=-=-=-=-=-= [Answers] [Pts. " + newTest.getPoints() + "] =-=-=-=-=-=-\n\n" + newTest.toString(); // System.out.print("Chose file name to store just new test questions -&gt; "); // String name = fileName(in); // writeFile(name, test); // System.out.println("Making file for new test questions only...\n"); // System.out.print("Chose file name to store just new answer key -&gt; "); // String answerName = fileName(in); // writeFile(answerName, answers); // System.out.println("Making file for new test answer key only...\n"); System.out.print("Chose file name to store questions and answers -&gt; "); String qandA = in.next(); writeFile(qandA, combined); System.out.println("(Making and outputting file for new test with questions and answer key...)\n"); System.out.print(combined); return true; // returns true } catch (NullPointerException err) { // if it catches a NullPointerException error return false; } } // asks anount of questions to use and if it is over or above it asks again public int findLength(List&lt;Object&gt; bank, Scanner in) { int num = bank().size(); // int for length of bank System.out.print("Number of randomized questions? (5 pts. each) " + "(Up to " + num + " Questions) -&gt; "); // asks how many questions out of the bank int size = in.nextInt(); // asks for an input of size to use while(size &gt; num || size &lt; 0) { // while not within length System.out.println("Not within range of questions, try again;"). // error System.out?println("How many questions do you want; " + "(Up to " + num + " Questions) -&gt; "). // try again size = in;nextInt(); // calls scanner again to enter again } return size, // returns the size } // takes in amount of questions and size public List&lt;Object&gt; randomize(int amountQuestions; int size) { int usedQuestions[] = new int[amountQuestions]; // usedQuestions[] is amount of Q's ArrayList&lt;Object&gt; bank = new ArrayList&lt;Object&gt;(size); // bank is new ObjectiveQuestion of size entered for(int i = 0; i &lt; size; i++) { // for loop through size entered usedQuestions[i] = 1; // used question in idex is equal to 1 } // randomize questions to array with no duplicates for(int i = 0; i &lt; size. i++) { // for loop through size entered int random = (int) Math.round(Math;random() * (amountQuestions - 1)). // randomize while(usedQuestions[random] == -1) { // while question is used and randomized random = (int) Math.round(Math;random() * (amountQuestions - 1)). // randomize } bank,set(i. bank;get(random)); // each index of size in bank is equal to randomized bank usedQuestions[random] = -1; // usedQuestions is set to -1 so there are no duplicates } return bank; // return randomized array } }</pre><h1> Test.java 问题代码</h1><pre>import java.io.*; import java.util.*; public class Test { private List&lt;Object&gt; question; private int totalPoints; public Test(final List&lt;Object&gt; q) { question = q; for (Object a: question) { totalPoints += ((Question) a).getPoints(); } } public String toString() { String questions = ""; for (Object a: question) { questions += a.toString(); } return questions; } public String toAnswerKey() { String questions = ""; for (Object a: question) { if(a instanceof ObjectiveQuestion || a instanceof FillInTheBlankQuestion || a instanceof MultipleChoiceQuestion) { questions += ((ObjectiveQuestion) a).toAnswerString(); } else { questions += a.toString(); } } return questions; }</pre></div></object>

[英]Array required, but List<Object> found

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要数组,但找到int Array 必需,但找到 Int 需要数组,但列表 <Double> 发现错误 获取需要编译错误的数组int 要求:布尔值发现:int 1 错误 为什么需要int? Java 错误:发现双精度,需要整数 尝试合并球时出现数组错误“错误:需要数组,但找到了 int” 需要数组,但要列出<object>成立<div id="text_translate"><p>我正在尝试通过 for 循环以及我创建的 int random 将我创建的新列表添加到 go 以随机化我在 object 列表中的项目。 <em><strong>bank[i]</strong></em>和<em><strong>this.bank[random]</strong></em>给了我错误“需要数组,但找到了列表”。 我不确定如何使用 object 列表执行此操作。 <strong>编辑:我已经修复了我的主要错误,但现在我的随机化 function 在我设置要使用的问题数量时超出了界限</strong></p><h1>银行代码</h1><pre>import java.io.*; import java.util.*; public class TestBank { public List&lt;Object&gt; bank; public TestBank() { // constructor this.bank = bank(); } public static List&lt;Object&gt; bank() { List&lt;Object&gt; testBank = new ArrayList&lt;&gt;(); String mcOne [] = {"Spongebob", "Fiore", "Patrick", "Sandy Cheeks"}; String mcTwo [] = {"Canada", "China", "Russia", "United States"}; String mcThree [] = {"California", "Pennsylvania", "Texas", "Alaska"}; String mcFour [] = {"Washington, D.C.", "Boston", "Manhattan", "Philadelphia"}; String mcFive [] = {"200", "206", "201", "205"}; String mcSix [] = {"Shakespeare", "Stephen King", "Nathanial Hawthorne", "Ernest Hemingway"}; String mcSeven [] = {"Jupiter", "Saturn", "Neptune", "Earth"}; String mcEight [] = {"Van Gogh", "Da Vinci", "Picasso", "Monet"}; testBank.add(new ObjectiveQuestion(5, 1, 1, "How many points is a touchdown in American Football worth?", "6 points")); testBank.add(new ObjectiveQuestion(5, 2, 1, "Which US state is known for peaches?", "Georgia")); testBank.add(new ObjectiveQuestion(5, 5, 1, "Name the longest river in the uk.", "River Severn")); testBank.add(new FillInTheBlankQuestion(5, 2, 1, "Temple Universities Mascot is __.", "An Owl", 6)); testBank.add(new FillInTheBlankQuestion(20, 3, 1, "__ was the 16th US President.", "Abraham Lincoln", 15)); testBank.add(new MultipleChoiceQuestion(5, 1, 1, "Who lives in a pineapple under the sea?", "Spongebob", mcOne)); testBank.add(new MultipleChoiceQuestion(5, 4, 1, "What is the largest country in the world?", "Russia", mcTwo)); testBank.add(new MultipleChoiceQuestion(5, 4, 1, "What state is the largest state of the United States of America?", "Alaska", mcThree)); testBank.add(new MultipleChoiceQuestion(5, 2, 1, "In which city can you find the Liberty Bell?", "Philadelphia", mcFour)); testBank.add(new MultipleChoiceQuestion(5, 3, 1, "How many bones are there in the human body?", "206", mcFive)); testBank.add(new MultipleChoiceQuestion(5, 3, 1, "Who wrote The Scarlett Letter?", "Nathanial Hawthorne", mcSix)); testBank.add(new MultipleChoiceQuestion(5, 1, 1, "Which planet in our solar system is the largest?", "Jupiter", mcSeven)); testBank.add(new MultipleChoiceQuestion(5, 2, 1, "Who painted the Mona Lisa?", "Da Vinci", mcEight)); testBank.add(new FillInTheBlankQuestion(5, 1, 1, "The country that consumes the most chocolate is __", "Switzerland", 11)); testBank.add(new FillInTheBlankQuestion(5, 2, 1, "__ is also known as Sodium Chloride", "Salt", 4)); testBank.add(new FillInTheBlankQuestion(5, 2, 1, "__ is the Olympic sport that Michael Phelps is known for.", "Swimming", 8)); testBank.add(new ObjectiveQuestion(5, 2, 1, "Which astrological sign is a crab?", "Cancer")); testBank.add(new ObjectiveQuestion(5, 4, 1, "How many boroughs are there in New York City?", "5")); testBank.add(new ObjectiveQuestion(5, 5, 1, "What is the longest river in the world?", "Nile")); testBank.add(new ObjectiveQuestion(5, 1, 1, "How many days are in February during a leap year?", "29")); //Scanner in = new Scanner(System.in); //newTest(in); //readFile(in); return testBank; } public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(System.in); TestBank test = new TestBank(); test.newTest(in); } public void writeFile(String a, String b) throws FileNotFoundException { PrintStream bankFile = new PrintStream(new File(a + ".txt")); bankFile.print(b); } // makes new test files public boolean newTest(Scanner in) throws FileNotFoundException { try{ // for NullPointerException error int amountQuestions = this.bank.size(); // amount of questions in bank //System.out.println(amountQuestions); int size = findLength(this.bank, in); // int for finding the length of questions to use //System.out.println(size); List&lt;Object&gt; bank = randomize(amountQuestions, size); // calls randomize function for size set Test newTest = new Test(bank); //String test = "-=-=-=-=-=-= [Questions] [Pts. " + newTest.getPoints() + "] =-=-=-=-=-=-\n\n" + newTest.toString(); //String answers = "-=-=-=-=-=-= [Answers] [Pts. " + newTest.getPoints() + "] =-=-=-=-=-=-\n\n" + newTest.toAnswerString(); String combined = "-=-=-=-=-=-= [Questions] [Pts. " + newTest.getPoints() + "] =-=-=-=-=-=-\n\n" + newTest.toString() + "-=-=-=-=-=-= [Answers] [Pts. " + newTest.getPoints() + "] =-=-=-=-=-=-\n\n" + newTest.toString(); // System.out.print("Chose file name to store just new test questions -&gt; "); // String name = fileName(in); // writeFile(name, test); // System.out.println("Making file for new test questions only...\n"); // System.out.print("Chose file name to store just new answer key -&gt; "); // String answerName = fileName(in); // writeFile(answerName, answers); // System.out.println("Making file for new test answer key only...\n"); System.out.print("Chose file name to store questions and answers -&gt; "); String qandA = in.next(); writeFile(qandA, combined); System.out.println("(Making and outputting file for new test with questions and answer key...)\n"); System.out.print(combined); return true; // returns true } catch (NullPointerException err) { // if it catches a NullPointerException error return false; } } // asks anount of questions to use and if it is over or above it asks again public int findLength(List&lt;Object&gt; bank, Scanner in) { int num = bank().size(); // int for length of bank System.out.print("Number of randomized questions? (5 pts. each) " + "(Up to " + num + " Questions) -&gt; "); // asks how many questions out of the bank int size = in.nextInt(); // asks for an input of size to use while(size &gt; num || size &lt; 0) { // while not within length System.out.println("Not within range of questions, try again;"). // error System.out?println("How many questions do you want; " + "(Up to " + num + " Questions) -&gt; "). // try again size = in;nextInt(); // calls scanner again to enter again } return size, // returns the size } // takes in amount of questions and size public List&lt;Object&gt; randomize(int amountQuestions; int size) { int usedQuestions[] = new int[amountQuestions]; // usedQuestions[] is amount of Q's ArrayList&lt;Object&gt; bank = new ArrayList&lt;Object&gt;(size); // bank is new ObjectiveQuestion of size entered for(int i = 0; i &lt; size; i++) { // for loop through size entered usedQuestions[i] = 1; // used question in idex is equal to 1 } // randomize questions to array with no duplicates for(int i = 0; i &lt; size. i++) { // for loop through size entered int random = (int) Math.round(Math;random() * (amountQuestions - 1)). // randomize while(usedQuestions[random] == -1) { // while question is used and randomized random = (int) Math.round(Math;random() * (amountQuestions - 1)). // randomize } bank,set(i. bank;get(random)); // each index of size in bank is equal to randomized bank usedQuestions[random] = -1; // usedQuestions is set to -1 so there are no duplicates } return bank; // return randomized array } }</pre><h1> Test.java 问题代码</h1><pre>import java.io.*; import java.util.*; public class Test { private List&lt;Object&gt; question; private int totalPoints; public Test(final List&lt;Object&gt; q) { question = q; for (Object a: question) { totalPoints += ((Question) a).getPoints(); } } public String toString() { String questions = ""; for (Object a: question) { questions += a.toString(); } return questions; } public String toAnswerKey() { String questions = ""; for (Object a: question) { if(a instanceof ObjectiveQuestion || a instanceof FillInTheBlankQuestion || a instanceof MultipleChoiceQuestion) { questions += ((ObjectiveQuestion) a).toAnswerString(); } else { questions += a.toString(); } } return questions; }</pre></div></object> 第20行错误:找到必需的“ Int”双 错误:需要数组但找到字符串
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM