当不应到达索引时,嵌套的for循环中的java.lang.IndexOutOfBoundsException

时间:2018-09-22 21:59:29

标签: java arraylist

package edu.bsu.cs222.twoWeekProject;

import java.util.ArrayList;

public class arraySorter {
public  arrayGroup sortByUsername(arrayGroup unsortedGroup) {
    ArrayList<String> array1 = unsortedGroup.array1;
    ArrayList<String> array2 = unsortedGroup.array2;
    ArrayList<String> noDuplicantsArray = removeDuplicantsFromArray(array1);
    String usernameTemp;
    String timestampTemp;
    int StartPosition;

    for(int i=0; i<noDuplicantsArray.size();i++) {
        StartPosition = getUsernameStartPosition(noDuplicantsArray.get(i), 
array1);
        for(int j=0; j<array1.size();j++) {
            if(noDuplicantsArray.get(i).equals(array1.get(j))) {
                usernameTemp = array1.get(j);
                timestampTemp = array2.get(j);
                array1.set(j, array1.get(StartPosition));
                array2.set(j, array2.get(StartPosition));
                array1.set(StartPosition, usernameTemp);
                array2.set(StartPosition, timestampTemp);
                StartPosition++;
            }
        }
    }
    arrayGroup finalArrays = new arrayGroup(array1, array2);
    return finalArrays;
}

public int getFrequency(String user, ArrayList<String> array) {
    int count = 0;
    for (int i = 0; i<array.size(); i++){
        if(array.get(i).equals(user)) {
            count++;
        }
    }
    return count;
}

public int getUsernameStartPosition(String user, ArrayList<String> array) {
    int count = 0;
    ArrayList<String> singleUserArray = removeDuplicantsFromArray(array);
    for(int i = 0; i<singleUserArray.size(); i++) {
        if(getFrequency(user, array) <= getFrequency(singleUserArray.get(i), 
array) && !singleUserArray.get(i).equals(user)) {
            count = count + getFrequency(singleUserArray.get(i), array);
        }
    }
    return count;
}

public boolean isHigherInAlphabet(int startAt, String a, String b) {
        char aPart = a.charAt(startAt);
        char bPart = b.charAt(startAt);
        if (Character.getNumericValue(aPart) < 
Character.getNumericValue(bPart)) {
            return true;
        } else if(Character.getNumericValue(aPart) == 
Character.getNumericValue(bPart)) {
            return isHigherInAlphabet(startAt+1,a,b);
        } else {
            return false;
        }
}

public ArrayList<String> removeDuplicantsFromArray(ArrayList<String> array) 
{
    ArrayList<String> returnArray = new ArrayList();
    for (int i=0;i<array.size();i++) {
        if(returnArray.contains(array.get(i))) {
        }else {
            returnArray.add(array.get(i));
        }
    }
    return returnArray;
}


}

我当前正在返回IndexOutOfBoundsException。该程序声称索引(StartPosition)太大而不能在数组列表中(如java.lang.IndexOutOfBoundsException:Index:11,Size:11),而StartPosition不应太大。该程序在以下行上特别失败:array1.set(j,array1.get(StartPosition));

编辑: 以下是来自我的testclass的代码,以便人们可以看到数组的确切样子...唯一失败的测试是testArraySorter()     公共类ArrayTests {     包edu.bsu.cs222.twoWeekProject;

import com.google.gson.*;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertTrue;


public class ArrayTests {

    private ArrayList<String> usersUnsorted = new ArrayList();
    private ArrayList<String> timestampsUnsorted = new ArrayList();
    private ArrayList<String> usersSorted = new ArrayList();
    private ArrayList<String> timestampsSorted = new ArrayList();
    private ArrayList<String> usersSingle = new ArrayList();

public void fillTestObjects() {
    for (int i = 1; i <= 5; i++) {
        usersUnsorted.add("User1");
        timestampsUnsorted.add("User1 Time" + Integer.toString(i));
        usersUnsorted.add("User2");
        timestampsUnsorted.add("User2 Time" + Integer.toString(i));
    }
    usersUnsorted.add("User2");
    timestampsUnsorted.add("User2 Time Final");


    usersSorted.add("User2");
    usersSorted.add("User2");
    usersSorted.add("User2");
    usersSorted.add("User2");
    usersSorted.add("User2");
    usersSorted.add("User2");
    usersSorted.add("User1");
    usersSorted.add("User1");
    usersSorted.add("User1");
    usersSorted.add("User1");
    usersSorted.add("User1");
    timestampsSorted.add("User2 Time1");
    timestampsSorted.add("User2 Time2");
    timestampsSorted.add("User2 Time3");
    timestampsSorted.add("User2 Time4");
    timestampsSorted.add("User2 Time5");
    timestampsSorted.add("User2 Time Final");
    timestampsSorted.add("User1 Time1");
    timestampsSorted.add("User1 Time2");
    timestampsSorted.add("User1 Time3");
    timestampsSorted.add("User1 Time4");
    timestampsSorted.add("User1 Time5");

    usersSingle.add("User1");
    usersSingle.add("User2");
}

@Test
public void testFillObjects() {
    fillTestObjects();
    ArrayList<String> usersSingleCopy = new ArrayList();
    usersSingleCopy.add("User1");
    usersSingleCopy.add("User2");

    Assert.assertTrue(usersSingleCopy.equals(usersSingle));
}



@Test
public void testArrayGroupEqual() {
    fillTestObjects();
    ArrayList<String> usersSingleCopy = new ArrayList();
    usersSingleCopy.add("User1");
    usersSingleCopy.add("User2");
    arrayGroup unsortedGroup = new arrayGroup(usersSingle, timestampsUnsorted);
    arrayGroup unsortedGroup2 = new arrayGroup(usersSingleCopy, timestampsUnsorted);

    Assert.assertTrue(unsortedGroup.isEqualTo(unsortedGroup2));

}
@Test
public void testArrayGroupNotEqual() {
    fillTestObjects();
    arrayGroup unsortedGroup = new arrayGroup(usersUnsorted, timestampsUnsorted);
    arrayGroup sortedGroup = new arrayGroup(usersSorted, timestampsSorted);

    Assert.assertFalse(unsortedGroup.isEqualTo(sortedGroup));

}
@Test
public void testSimilarArrayGroupNotEqual() {
    fillTestObjects();
    arrayGroup unsortedGroup = new arrayGroup(usersUnsorted, timestampsUnsorted);
    arrayGroup sortedGroup = new arrayGroup(usersUnsorted, timestampsSorted);

    Assert.assertFalse(unsortedGroup.isEqualTo(sortedGroup));

}


@Test
public void testArraySorter() {
    fillTestObjects();
    arrayGroup unsortedGroup = new arrayGroup(usersUnsorted, timestampsUnsorted);
    arrayGroup sortedGroup = new arrayGroup(usersSorted, timestampsSorted);

    arraySorter arraysorter = new arraySorter();

    Assert.assertTrue(sortedGroup.isEqualTo(arraysorter.sortByUsername(unsortedGroup)));
}

@Test
public void testDuplicantRemoval() {
    fillTestObjects();
    arraySorter arraysorter = new arraySorter();
    Assert.assertTrue(usersSingle.equals(arraysorter.removeDuplicantsFromArray(usersUnsorted)));
}

@Test
public void testDuplicantRemovalSize() {
    fillTestObjects();
    arraySorter arraysorter = new arraySorter();
    Assert.assertEquals(arraysorter.removeDuplicantsFromArray(usersUnsorted).size(), 2);
}

@Test
public void testHigherInAlphabet() {
    fillTestObjects();

    arraySorter arraysorter=new arraySorter();
    Assert.assertTrue(arraysorter.isHigherInAlphabet(0, "Joa", "Job"));
}

@Test
public void testLowerInAlphabet() {
    fillTestObjects();

    arraySorter arraysorter=new arraySorter();
    Assert.assertFalse(arraysorter.isHigherInAlphabet(0, "Job", "Joa"));
}

@Test
public void testStartPositionUser1() {
    fillTestObjects();

    arraySorter arraysorter=new arraySorter();
    Assert.assertEquals(6, arraysorter.getUsernameStartPosition("User1", usersUnsorted));
}
@Test
public void testStartPositionUser2() {
    fillTestObjects();

    arraySorter arraysorter=new arraySorter();
    Assert.assertEquals(0, arraysorter.getUsernameStartPosition("User2", usersUnsorted));
}

@Test
public void testUser2Frequency() {
    fillTestObjects();

    arraySorter arraysorter=new arraySorter();
    Assert.assertEquals(6, arraysorter.getFrequency("User2", usersUnsorted));
}
@Test
public void testUser1Frequency() {
    fillTestObjects();

    arraySorter arraysorter=new arraySorter();
    Assert.assertEquals(5, arraysorter.getFrequency("User1", usersUnsorted));
}

}

0 个答案:

没有答案