Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help with increasing size of an array

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with increasing size of an array

    Hello all,

    I'm working on an assignment that says the following.

    "
    1. The array size is fixed in Listing 10.6. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it.
    2. Implement the dropStudent method.
    3. Add a new method named clear() that removes all students from the course.

    Write a test program that creates a course, adds three students, removes one, and displays the students in the course."

    10.6 Listing
    public class Course {
     
    	private String courseName;
    	private String[] students = new String[100];
    	private int numberOfStudents;
     
    	}
     
    		public Course(String courseName) {
    		this.courseName= courseName;
    	}
     
    	public void addStudent(String student) {
    		students[numberOfStudents] = student;
    		numberOfStudents++;
    	}
     
    	public String[] getStudents() {
    		return students;
    	}	
     
    	public int getNumberOfStudents() {
    		return numberOfStudents;
    	}	
     
    	public String getCourseName(){
    		return courseName;
    	}
     
    	public void dropStudent(String student) {
    		//Left as exercise;
    	}
     
     
    	}
     
     
     
    }

    My Test Code based off of book
    public static void main(String[] args) {
    		Course course1= new Course("Business");
     
     
    		course1.addStudent("Jay");
    		course1.addStudent("Silent Bob");
    		course1.addStudent("Dante");
     
    		course1.dropStudent("Jay");
     
    		System.out.println("Number of students in course1: " +course1.getNumberOfStudents());
    		String[] students= course1.getStudents();
    		for (int i=0; i< course1.getNumberOfStudents(); i++)
    			System.out.println(students[i] + " , ");}
     
     
    	}

    My adjusted 10.6

    public class Course {
     
    	private String courseName;
    	private String[] students = new String[100];
    	private int numberOfStudents;
     
    	}
     
    		public Course(String courseName) {
    		this.courseName= courseName;
    	}
     
    	public void addStudent(String student) {
    		students[numberOfStudents] = student;
    		numberOfStudents++;
    	}
     
    	public String[] getStudents() {
    		return students;
    	}	
     
    	public int getNumberOfStudents() {
    		return numberOfStudents;
    	}	
     
    	public String getCourseName(){
    		return courseName;
    	}
     
    	public void dropStudent(String student) {
    		students[numberOfStudents] = student;
    		numberOfStudents--;
    	}
     
    	public void Clear(){
    		student.length= 0;
    	}
     
     
     
    }

    The problem I'm having is, for the first part of the question where I need to automatically increase the array size. I'm really not great at this stuff. I have tried breaking it down, but can't "get it", I guess.

    I assume, it'd be a loop that checks to see if the student array is full and if so, do the increaseArray() part, by maybe multiplying the student array and then assigning it. I just don't know how to do it haha. Any advice or breadcrumbs would be appreciated.

    My *best* attempt at the loop so far has been

    if (students == students.length){
    int bigArray = 2*students.length;
    String increaseArray()= new String[students];
    System.arraycopy(students, 0, increaseArray, 0, students.length);
    students= increaseArray;

    but,yeah... Doesn't seem to be right.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default re: Help with increasing size of an array

    Welcome to the forum! Thanks for taking the time to learn to post code correctly.

    Please give your threads better titles. I'll change this one for you.

    Why/how is the general algorithm for increasing the size of the array that you posted not working? Show the code for just that part, if it exists.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Osmo (February 5th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with increasing size of an array

    Oh, sorry about that. I'm bad at titles. Meant to go back and redo, but forgot!

    This is the part. After this section, it's just like the one above.

    public class Course {
     
    	private String courseName;
    	private String[] students = new String[100];
    	private int numberOfStudents;
     
    	}
     
    	if (students == students.length){
    		bigArray= 2*students.length;
    		String increaseArray()= new String[students];
    		System.arraycopy(students, 0, increaseArray, 0, students.length);
    		students= increaseArray;
    	}


    *edit* Prematurely posted!
    Maybe the placement is wrong or maybe I'm doing method wrong to use for the TestCourse or not really understanding the call aspect. To make sure, once I make the methods and such, do I then copy and use it on the TestCourse? If not, that would explain a lot of my confusion. It's been a couple years since my last Java class, so I'm still remembering stuff.

    I get the errors at if, int bigArray, and String IncreaseArray.

    if- invalid AnnotationName
    Int bigArray- says to put a }, but then when I do, more and more errors show up.
    Increase- same as above

  5. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Help with increasing size of an array

    Quote Originally Posted by Osmo View Post
    The problem I'm having is, for the first part of the question where I need to automatically increase the array size. I'm really not great at this stuff. I have tried breaking it down, but can't "get it", I guess.

    I assume, it'd be a loop that checks to see if the student array is full and if so, do the increaseArray() part, by maybe multiplying the student array and then assigning it. I just don't know how to do it haha. Any advice or breadcrumbs would be appreciated.
    You have an array students that has a physical "capacity" and you have a variable numberOfStudents that contains the "logical" count of students.

    If the capacity of the array is not sufficient for the new logical count (numberOfStudents plus 1), you must allocate a new array. This is a simple test, not a loop or something else!

    And if you want to do this in a "good" way, define a method named e.g. ensureCapacity that receives the minimum capacity that the array must have in order to continue without problems. And in addStudent you have just to invoke:

    	public void addStudent(String student) {
    		ensureCapacity(numberOfStudents+1);          // this is up to you
    		students[numberOfStudents] = student;
    		numberOfStudents++;
    	}

    By the way, this is exactly the same concept used under the hood in standard collections like java.util.ArrayList.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. The Following User Says Thank You to andbin For This Useful Post:

    Osmo (February 5th, 2014)

  7. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with increasing size of an array

    Quote Originally Posted by andbin View Post
    You have an array students that has a physical "capacity" and you have a variable numberOfStudents that contains the "logical" count of students.

    If the capacity of the array is not sufficient for the new logical count (numberOfStudents plus 1), you must allocate a new array. This is a simple test, not a loop or something else!

    And if you want to do this in a "good" way, define a method named e.g. ensureCapacity that receives the minimum capacity that the array must have in order to continue without problems. And in addStudent you have just to invoke:

    	public void addStudent(String student) {
    		ensureCapacity(numberOfStudents+1);          // this is up to you
    		students[numberOfStudents] = student;
    		numberOfStudents++;
    	}

    By the way, this is exactly the same concept used under the hood in standard collections like java.util.ArrayList.
    Sorry, I'm (really) slow at this.

    I'll mess around for a bit and get back to you! Thanks =)

    *edit*
    Hm... I've tried a few things and I keep getting an error when done:

    "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Course.addStudent(Course.java:19)
    at TestCourse.main(TestCourse.java:9)
    "

    Seems as though, I'm still doing the increaseArray part wrong and it is just staying at 1.
    Last edited by Osmo; February 5th, 2014 at 06:33 PM. Reason: Didn't want to double post

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with increasing size of an array

    Show the code. We're not omniscient.

Similar Threads

  1. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  2. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 09:35 PM
  3. [SOLVED] Array[] Size Question
    By StephenCoyle in forum Java Theory & Questions
    Replies: 5
    Last Post: March 24th, 2013, 08:41 AM
  4. Increasing the column size in JTabe
    By overdriveboy in forum AWT / Java Swing
    Replies: 1
    Last Post: May 30th, 2012, 10:26 AM
  5. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM