Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array lengths

 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
i have an array of values which refrence String objects, and i need to increment the unique value if the element in the array is not null which it all works fine when i set the array size to say three, but if i increase it to say 10 it crashses. any ideas on my problem...
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
array has a fixed length

try ArrayList instead
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see what the ArrayList suggestion has to do with the original problem.

but if i increase it to say 10 it crashses.


How about some details about what "crashes" means? Post some code. Post your error. No one here is a mind reader.
 
Martyn Clark
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the lack of info
any way here is the error if i set the array size 10.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at arrays.Storage.addStudent(Student.java:69)
at arrays.StudentTest.main(StudentTest.java:19)
this happens after i have added five new names to the array

here is the add method i have:

public void addStudent(Student studentObj, int count)
{

int hash = studentObj.hashCode(numOfStudents);
int index = 0;
boolean add = false;
if (students[hash] != null)
{
while (students[hash] != null && hash < students.length)
{
++hash;
System.out.println("++i");
}
students[hash] = studentObj;
}
else
{

students[hash] = studentObj;
}

and the test:
class StudentTest
{

public static void main(String[] args)
{
int maxNum = 10;
Storage store = new Storage(maxNum);

for (int i = 0; i < store.getSize(); i++)
{
Student name = new Student(JOptionPane.showInputDialog(
"please enter a name!").toUpperCase(), JOptionPane
.showInputDialog("please enter a course!")
.toUpperCase());
store.addStudent(name, maxNum);
}

System.out.println("Student name\tStudent course");
System.out.println("------------\t--------------");
for (int i = 0; i < store.getSize(); i++)
{
System.out.println( store.retrieveList(i).getName() + "\t\t"
+ store.retrieveList(i).getCourse());
}
System.exit(0);

}

}

thanks again
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see what the ArrayList suggestion has to do with the original problem.

i understood OP was trying to add new items to an array
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic