It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

A little help with a debug..

A little help with a debug..

Postby Grouchotron on Fri Dec 22, 2006 4:18 am

Hi, I'm a senior in high school taking a java class as an elective. I'm having a lot of fun learning, but recently I have come across a problem. While learning about arrays I noticed that unlike javascript in java you can't change to length of arrays after they have been initialized. I tried to write a class that I could use to lengthen arrays, but I am having a little trouble, and my teacher can't seem to find my problem. I'm going to post what I have written so far, using bluejay, and if you could point out my mistake I would be greatly appreciative.


The example class is a driver that should show how the arrayadjust class changes the length of the array.

public class example
{
public static void main(String[]args)
{

arrayadjust arrayadjuster = new arrayadjust();

int[] array = new int[10];



arrayadjuster.adjustint(array,15);

System.out.println(array.length);
}
}


The arrayadjust class uses the copyarray class.

public class arrayadjust
{
void adjustint(int[] array, int newlength)
{
copyarray arraycopier = new copyarray();

int oldlength = array.length;

int[] temparray = new int[oldlength];

arraycopier.copyiray(array,temparray);

array = null;

array = new int[newlength];

arraycopier.copyiray(temparray,array);
}
}


This class just copies one array onto another, I'm fairly certain that it works as it should.

public class copyarray
{
void copyiray(int[] array1, int[] array2)
{
for(int i = 0; i<array2.length && i<array1.length; i++)
{
array2[i] = array1[i];
}
}

void copydray(double[] array1, double[] array2)
{
for(int i = 0; i<array2.length && i<array1.length; i++)
{
array2[i] = array1[i];
}
}

void copysray(String[] array1, String[] array2)
{
for(int i = 0; i<array2.length && i<array1.length; i++)
{
array2[i] = array1[i];
}
}

void copycray(char[] array1, char[] array2)
{
for(int i = 0; i<array2.length && i<array1.length; i++)
{
array2[i] = array1[i];
}
}
}

I hope this isn't too overwhelming of a problem to post here. I also hope there isn't some incredibly simple answer that will make it seem as if I am wasting your time. I think my problem has something to do with how arrays are passed, whether they are passed as aliases or not, or something like that, but I'm not sure.


P.S. When I run this I do not get an error message, but the array doesn't get changed.
Grouchotron
 
Posts: 9
Joined: Fri Dec 22, 2006 2:40 am

Postby meongmania on Fri Dec 22, 2006 6:37 am

of course the array doesn't change. Your classes and methods doesn't do any changing on the array in the example class.

the concept of this program, as i read it, is to read some array and then creates a new one, with new size defined by the user and have the same values as the previous array, am i right?

this are some suggestions that u should consider :
1. if you want to do something like that (3 classes, and the 2 class changes something in the first class), don't use void methods, use return type methods instead. or you still can use void, but the variable that u want to change, as array (above) for an example, refers to the variables that u want to change, not the variable inside the void method if the method itself are inside a different class as the variable that u want to change. as above, u r trying to modify an array variable inside the adjustint method, but u don't realize that u only changing the array variable that is inside the method, not the original array variable in the example class! so u see, one of the reason the array doesn't get changed is that u are trying a void type methods to be used as a tool to change variables outside their own classes. The simplest way to correct this is using arrays as a return type for the methods.

2. why arrays? if you want some data types that has the ability to change sizes why dont u use ArrayList or Vector? arrays are object in java, same as Strings, if u change the content or value of a string for example, what u actually doing is cr8 a new string that contains the new value and dispose the old one.

3. try to learn the international standard of naming and writing classnames, methods, and variables, so ur code will be much easier to read by other java programmers out there : )
meongmania
 
Posts: 16
Joined: Tue Dec 19, 2006 2:58 am

Postby Grouchotron on Fri Dec 22, 2006 7:33 pm

Sorry, being new to java I don't always write perfectly in the standard, but im trying ; )

Thanks for the reply, I appreciate the help.

However, I was trying to make it so that I could increase the size of an array without having to create a new one. My goal was to pass an array to a method that would copy it onto a temporary array, assign to null, recreate it with more elements, and recopy the data back into it.

I understand that the methods were only changing the arrays in them, but I thought that when you pass an array, or any other object, that you were only creating a new reference to the same object, and therfore any changes you made to the new alias would happen to the original array in the driver class as well.

I'm fairly certain that this concept is at least partially valid, because when I tested my copyarray class it worked, I was able to pass an empty array the the copyarray class and copy data into it without returning the value. When I printed out the array's elements with the driver class I saw that they were indeed changed.


I know that using a return statement would be easier, but I would of course have to create a new array in the driver class to take on the returned value, and this would defeat the purpose of the project (because I could have just created a new array in the driver class in the first place).


Also, you mentioned arraylists and vectors. I have to admit that I'm not sure what these are, but I will look into them. It would be great if there was already something in place that could do what I'm trying to make my program do.
Grouchotron
 
Posts: 9
Joined: Fri Dec 22, 2006 2:40 am

Postby meongmania on Sat Dec 23, 2006 4:27 am

Grouchotron wrote:
However, I was trying to make it so that I could increase the size of an array without having to create a new one. My goal was to pass an array to a method that would copy it onto a temporary array, assign to null, recreate it with more elements, and recopy the data back into it.

I understand that the methods were only changing the arrays in them, but I thought that when you pass an array, or any other object, that you were only creating a new reference to the same object, and therfore any changes you made to the new alias would happen to the original array in the driver class as well.

I'm fairly certain that this concept is at least partially valid, because when I tested my copyarray class it worked, I was able to pass an empty array the the copyarray class and copy data into it without returning the value. When I printed out the array's elements with the driver class I saw that they were indeed changed.



no no no, your concept is not 'partially' valid, it is valid. when you're working with an object which refers or modifies other object variables (we should call it instance variables). the object referred to should change its state, depends on how and what you're changing. however, since the 'array' variable you're changing is not an instance variable (it's inside a method on another class), you can only change its value or behavior inside the method itself. example :

if you define a variable inside a method (i call it method variable) like this :

public class Sample1{
//...
public void methodSample1(){
int someNumber = 100;
//...
}
}

you cannot change the someNumber variable from anywhere outside the methodSample1() method, that works the same as your array : ). it's because the variable are active only when the methodSample1() is running. if in some cases the variable is to be accessible by other methods in the same class, or by methods on other classes you could write it like this :
// class one :
public class Sample1{
public int someNumber;
//...
public void methodSample1(){
someNumber = 100;
//...
}
}

or, if we implements encapsulation, it should be written like this :
//class two
public class Sample1{
private int someNumber;
//...
public void setNumber(int number){
someNumber = number;
}

public int getNumber(){
return someNumber;
}

public void methodSample1(){
someNumber = 100;
//...
}
}

if we write it like this, the someNumber variable which is both written above as an instance variable can be accessed from outside their classes. by writing :

Sample1 aSample = new Sample1();
aSample.someNumber = 150; // if we're using class one
aSample.setNumber(150); // if we're using class two

since your array are defined inside a static method, there are ways so that it can be accessed from outside the class and outside the method, such as :

public class Sample2{
public static int someNumber; // a static method can only access a static variable
//...
public static void main(String[]args){
someNumber = 150;
//...
}
}

we can access the variable directly by defining the owner class :
Sample2.someNumber = 500;
meongmania
 
Posts: 16
Joined: Tue Dec 19, 2006 2:58 am

Postby meongmania on Sat Dec 23, 2006 5:01 am

this is the implementation to your program :

public class Example {
public static int[]theArray;

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

for(int i=0;i<10;i++){
theArray[i] = i;
}

ArrayAdjust adjuster = new ArrayAdjust();

adjuster.adjustArray(15);

for(int i=0;i<theArray.length;i++){
System.out.println(theArray[i]);
}
}
}

class ArrayAdjust{
public void adjustArray(int size){
int[]storage = new int[size];
copyArray(Example.theArray,storage);
Example.theArray = storage;
}

public void copyArray(int[]source,int[]destination){
for(int i = 0; i<source.length && i<destination.length; i++){
destination[i] = source[i];
}
}
}
meongmania
 
Posts: 16
Joined: Tue Dec 19, 2006 2:58 am

Postby Grouchotron on Sat Dec 23, 2006 6:33 am

Thanks much!
Grouchotron
 
Posts: 9
Joined: Fri Dec 22, 2006 2:40 am


Who is online

Users browsing this forum: No registered users and 2 guests