如何获取ArrayList中的索引数?

时间:2018-03-26 20:18:14

标签: java arraylist indexing

我有一个非常简单的ToDO列表的以下代码。

这是我创建新任务的课程:

import java.util.Scanner;

public class Task {
    String tascDesc;
    Scanner scanner = new Scanner(System.in);   

    public Task() {}

    //Get the description from user
    public  String setTaskDesc() {
        System.out.println("Enter a task: ");
        tascDesc = scanner.nextLine();
        return tascDesc;
    }

为任务创建新列表的类:

import java.util.ArrayList;

public class aList {
    //Create a new List
    ArrayList<String> myList = new ArrayList<>();
    Task task = new Task();

    //Add task to the List
    public void AddToList() {
        myList.add(task.setTaskDesc());
        System.out.println("New item " + task.tascDesc + " is added in your list!");
    }

    //Print the list with numbers
    public void PrintList(){
        for (int i = 0; i < myList.size(); i++) {
            int index = myList.indexOf(task.tascDesc);
            System.out.println(index + "." + task.tascDesc);
        }
    }
}

该程序的主要/测试类:

import java.util.Scanner;

public class ThingsUHave2Do {
    public static void main(String[] args) {
        aList myList = new aList();
        Scanner scanner = new Scanner(System.in);
        int toDo = 1;

        while (toDo != 3) {
            System.out.println("Enter 1 for create a task, 2 to print your list, 3 to exit.");
            toDo = scanner.nextInt();
            if (toDo == 1) {
                myList.AddToList();
            }
            if (toDo == 2) {
                System.out.println(myList.myList);
                myList.PrintList();
            }
        }
    }
}

很抱歉,如果我的代码行太多,但这是我找到解释问题的唯一方法。所以一切都按照我想要的方式工作,除了当我想要打印列表时,我想要一个输出:

  1. 买糖
  2. 学习java
  3. 感谢大家的帮助。
  4. 我无法将索引编号作为正常编号。我做了很多次尝试,但我没有达到预期的效果。

3 个答案:

答案 0 :(得分:0)

这是一个示例应用程序,可以演示您的想法。

  

CustomTask Class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication48;

/**
 *
 * @author blj0011
 */
public class CustomTask
{

    String tascDesc;
    String priority;

    public CustomTask(String tascDesc, String priority)
    {
        this.tascDesc = tascDesc;
        this.priority = priority;
    }

    public CustomTask(String tascDesc)
    {
        this.tascDesc = tascDesc;
        this.priority = "Low";
    }

    public void setTascDesc(String tascDesc)
    {
        this.tascDesc = tascDesc;
    }

    public String getTascDesc()
    {
        return this.tascDesc;
    }

    public void setPriority(String priority)
    {
        this.priority = priority;
    }

    public String getPriority()
    {
        return priority;
    }

    @Override
    public String toString()
    {
        return tascDesc + " : " + priority;
    }

}
  

Main(用于测试CustomTask类)

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author blj0011
 */
public class JavaApplication48
{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
        List<CustomTask> customTasks = new ArrayList();

        //One way to add to list
        CustomTask task1 = new CustomTask("first task!", "High");
        customTasks.add(task1);

        //Another way to add to list
        customTasks.add(new CustomTask("second task!")); //using this constructor automatically sets the priority to low

        //One way of printing list
        System.out.println("One way of printing list");
        for (int i = 0; i < customTasks.size(); i++) {
            System.out.println("Task " + i + ": " + customTasks.get(i).getTascDesc() + " - Priority: " + customTasks.get(i).getPriority());
        }

        //Another way to print list
        System.out.println("\nAnother way to print list");
        for (CustomTask task : customTasks) {
            System.out.println("Task : " + task.getTascDesc() + " - Priority: " + task.getTascDesc());
        }

        //Print using toString method
        System.out.println("\nPrint using toString method");
        for (CustomTask task : customTasks) {
            System.out.println(task);
        }

    }

}
  

输出

run:
One way of printing list
Task 0: first task! - Priority: High
Task 1: second task! - Priority: Low

Another way to print list
Task : first task! - Priority: first task!
Task : second task! - Priority: second task!

Print using toString method
first task! : High
second task! : Low
BUILD SUCCESSFUL (total time: 0 seconds)

答案 1 :(得分:-3)

只需更改此行:

            System.out.println(index + 1 + "." + task.tascDesc);

答案 2 :(得分:-3)

            System.out.println(i + 1 + "." + myList.get(i));