많은 코드가 있지만 여기에 맞는 비트가 있습니다.알 수없는 크기의 int 배열
나는 스텁으로 크기가 10 인 배열을 가지고 있는데, 어떤 크기의 배열을 얻을지 미리 알지 못한다. 단지 10보다 클 수 없다. 이론적으로 위의 코드에서
int[] arrTracker = new int[10];
arrTracker = MyLibrary.SearchForABookISBN(true, AShelf, userParsedArr, aUserTitle);
for (int j = 0; j < 10; j++)
{
pln(AShelf[arrTracker[j]]);
}
내가 그것을 통해 내 배열 I 루프를 가지고 있고 MyLibrary를 객체 배열에 내가 검색하고 책을 발견 한 경우 기본적으로 될 것입니다 내용을 (표시하면.
"userParsedArr는"사용자가 ISBN을 입력합니다. 여기
public int[] SearchForABookISBN (boolean aFlag, Book[] anArray, int[] AnISBN, String aTitle)
{
//This one was difficult.
int length = anArray.length;
int[] AnotherArr = new int[0];
int[] AnotherArrTemp = new int[0];
int[] AnotherArrNew = new int[0];
boolean UFoundMe = false;
if (aFlag == true)
{
for (int i = 0; i < length; i++)
{
//Assume we find the ISBN
if ((anArray[i].Title.equals(aTitle)))
{
int counter = 0;
for (int j = 0; j < 9; j++)
{
if (anArray[i].ISBN[j] == AnISBN[j])
{
counter++;
}
else
{
UFoundMe = false;
break;
}
if (counter == 9)
{
UFoundMe = true;
}
}
if (UFoundMe == true)
{
//Create our 'main' tracker at 1 + size of previous array.
AnotherArrNew = new int[1 + AnotherArr.length];
if (AnotherArrTemp.length > 0)
{
//Copy values into a temp array.
//Make a new temp array
for (int m = 0; m < AnotherArr.length - 1; m++)
{
AnotherArrNew[m] = AnotherArrTemp[m];
}
}
AnotherArrNew[(AnotherArrNew.length) - 1] = i;
AnotherArrTemp = new int[AnotherArrNew.length];
for (int n = 0; n < AnotherArr.length; n++)
{
AnotherArrTemp[n] = AnotherArrNew[n];
}
System.out.println(anArray[i]);
}
}
}
}
return AnotherArrNew;
}
}
기본 아이디어는 좀 빈 배열을 만들고, 내가 책을 발견하면 그때는 새로운 배열 한 사이즈 큰 만들고 기존의 배열을 던져이다, 임시 배열에 내용을 전송 한 다음주의 깊게 바를 만듭니다. 이전 어레이를 삭제하기 전에 내가 만든 것의 ckup을 사용하여 더 크게 만듭니다.
아마도 내가 10 권의 책을 가지고 있고, 3 권에는 동일한 제목과 ISBN이 있다고 할 수 있습니다. 나는 3 개의 배열을 반환 할 것으로 기대하지만, 미리 알지는 못한다. 왜냐하면 내가 20 권의 책이 주어지고 그것들 모두가 같기 때문이다.
내가 미리 가져 오는 배열의 크기를 알 수 있도록 MyLibrary.SearchForABookISBN (true, AShelf, userParsedArr, aUserTitle) .length가 작동합니까? 그러니 그냥 선언
int aLength = MyLibrary.SearchForABookISBN(true, AShelf, userParsedArr, aUserTitle).length
int[] arrTracker = new int[aLength];
미리 크기를 모르는 경우 ArrayList를 사용하십시오. 필요한 경우 배열로 변환 할 수 있습니다. – jlordo