?
Solved

Add to string array

Posted on 2010-03-27
12
Medium Priority
?
320 Views
Last Modified: 2012-05-09
I know the size of my array but how do I know where to put it in?

Let say I got

House[] house    //size 4

now I want to do this

public void addHouse(House myhouse){
  How do I know where to put it in. I dont wanna write over someting
that already exists.
      
}
0
Comment
Question by:Mickeys
  • 6
  • 6
12 Comments
 
LVL 23

Expert Comment

by:cmalakar
ID: 28769903
You need to maintain a instance variable which keeps track of how many objects have added.
and use that to update the array.

Ex..

Class X{
int nextArrayPosition = 0;
House[] house = new House[10];

public void addHouse(House myhouse){
house[nextArrayPosition] = myhouse;
//Now increment the nextArrayPostion
nextArrayPosition = nextArrayPosition + 1;
}
}
0
 

Author Comment

by:Mickeys
ID: 28770639
so there is no other way? I dont know how have written this exercises. They dont seem to know java though.
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 28770798
>> so there is no other way?

Are you asking that.. you still dont know the size of the array ?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:Mickeys
ID: 28772064
well what i have done is brought the size into the constructor now and done this

public class HouseLIst{

      private House[] houses;
      static int nextArrayPosition = 0;
      
      public HouseList(int size){
            houses= new House[size];
      }
      
but I get this:
org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 28772451
the above is perfect.. problem somewhere .

Complete program may help us.
0
 

Author Comment

by:Mickeys
ID: 28772523
Mail? I can send it in a zip.
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 28772622
can't attach here ?
0
 

Author Comment

by:Mickeys
ID: 28772695
No it always complains about format and stuff. So fastest way mail. Would be kind if that worked for you
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 28772871
I am not very sure.. if sending to mails.. violates the ee rules.
0
 

Author Comment

by:Mickeys
ID: 28773324
it doesnt. Anyway...check your gmal. :-)
0
 
LVL 23

Accepted Solution

by:
cmalakar earned 2000 total points
ID: 28773953
First obversation.. found this.

SIZE variable is never assigned with a value.  And you array size is 0, that might be the problem.

>> ResultList resultList= new ResultList(SIZE);

should be

 ResultList resultList= new ResultList(rawResults.length);   // right ?
0
 

Author Comment

by:Mickeys
ID: 28774192
You are so right. Been sitting here since the morning so you get blind. So now I onyl got the sort. I have tried diffrent things but ending up with deleting the code. Do you got any suggestions?
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question