2D数组索引超出范围异常问题

时间:2015-10-07 14:57:52

标签: java arrays

我正在制定计划以制定时间表;我已经通过将员工添加到列表来接受用户输入以获取员工,但是当我尝试将项目从列表移动到双数组时,我得到了一个数据索引超出界限异常。我能在这里做些什么来解决这个问题,我已经尝试过其他我能想到的一切。

import java.util.*;

public class schedule
{

    public static void main(String[] args)
    {
        Scanner readme = new Scanner(System.in);
        List<String> employees = new ArrayList<String>();
        //accepts input for list
        while(true)
        {
            System.out.println("Employees are: " + employees);
            System.out.println("Are there more? (y/n)");
            if (readme.next().startsWith("y"))
            {
                System.out.println("Who?");
                employees.add(readme.next());
            }
            else
            {
                //creates a double array the size of the employees list by the number of shifts in the week
                String[][] scheduleArr = new String[employees.size()][14];
                //adds items from array to corresponding spot on double array
                for (int i = 0; i <= scheduleArr.length; i++)
                {
                    scheduleArr[i][0] = employees.get(i);
                }
                System.out.print(scheduleArr[0][0]);
            }
        }

    }
}

3 个答案:

答案 0 :(得分:2)

数组索引从0开始,以长度结束 - 1。

更改

        ListView
        {
            id: ueUserInfoListView

            antialiasing: true
            Layout.alignment: Qt.AlignCenter
            Layout.fillWidth: true

            Layout.preferredHeight: 128

            clip: true

            spacing: 64

            model: uePeopleModel

            orientation: ListView.Horizontal

            highlightFollowsCurrentItem: false

            delegate: Image
            {
                id: ueUserInfoListViewDelegate

                source: "image://uePeopleModel/"+model.ueRoleImage

                function ueDoOpacity()
                {
                    if(ueUserInfoListViewDelegate===currentItem)
                        opacity=1.0
                    else
                        opacity=0.3
                }

                Behavior on opacity
                {
                    NumberAnimation
                    {
                        duration: 1000
                    }   // NumberAnimation
                }   // Behavior

                Component.onCompleted:
                {
                    ueUserInfoListViewDelegate.focusChanged.connect(ueDoOpacity)
                }   // Component.onCompleted
            }   // delegate

            Component.onCompleted:
            {
                preferredHighlightBegin=width/2
                preferredHighlightEnd=width /2
                highlightRangeMode=ListView.StrictlyEnforceRange
                currentIndex=count/2
            }   // Component.onCompleted
        }   // ListView

for (int i = 0; i <= scheduleArr.length; i++)

答案 1 :(得分:1)

你应该从0迭代到for (int i = 0; i < scheduleArr.length; i++) 而不是length-1(数组基于0)

length

答案 2 :(得分:1)

将代码修改为

for (int i = 0; i < scheduleArr.length; i++) //NOT i <= scheduleArr.length
        {
            scheduleArr[i][0] = employees.get(i);
        }

for (int i = 1; i <= scheduleArr.length; i++) //NOT i <= scheduleArr.length
        {
            scheduleArr[i][0] = employees.get(i);
        }

两者都会满足您的需求!