Jump to content

array.lengths trouble...again..

- - - - -

  • Please log in to reply
4 replies to this topic

#1
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
hello... i made another code as one of my exercises... it goes like this...

 public static void main (String args []) {

    	int[] array={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

    	for (int c=0; c<20; c++)

    		System.out.print (array[c]+" ");

    		System.out.println ("");


    	String a=JOptionPane.showInputDialog ("Enter number search:");

    	int search=Integer.parseInt (a);


    	for (int b=0; b<20; b++){

    		if (array[b]==search)

    			break;

    	if (b==20)

    		System.out.println (search+" not found!");

    	else if (search>20)

			System.out.println (search+" not found!");

    	else

    		System.out.println ("Found "+search);

    		break;

    	}


    		for (int b=0; b<array.length; b++){

    			if (array[b]==search)

    				break;

    		for (int k=b; k<array.length; k++)

    			array[k]=array[k+1];

    			array.length--;

    			}


    	for (int f=0; f<20; f++)

    		System.out.print (array[f]+" ");

    		System.out.println ("");


    }



}

but there is an error saying that cannot assign a value to final variable length...
help again???
thanks a lot!!!!

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

array.length--; 

You can't reduce an array's size. Your array has a size of 20, and that's never going to change.

And your next question will be:

Quote

I have an IndexOutOfBoundsException. Help?

for (int k=b; k<array.length; k++) 

                array[k]=array[k+1]; 

If you access the array with k+1 in there you need to change the loop condition to k<array.length-1
or else you'll do array[20] in the end when the max is 19 (array's size is 20, but the index starts with 0 --> 19 is the max index)

#3
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I don't understand what you want to do with this code.
for (int k=b; k<array.length; k++)
                array[k]=array[k+1];
                array.length--;
                } 

Anyway, you can't change the variable length because it's assigned when the array is created and it can't be modified.

Also, this is wrong:

if (b==20)
            System.out.println (search+" not found!");
        else if (search>20)
            System.out.println (search+" not found!");
        else
            System.out.println ("Found "+search);
            break;
        } 

The right way to do that is:

       for (int b=0; b<20; b++){
            if (array[b]==search)
                break;
       }
       if (search>=20)
            System.out.println (search+" not found!");
        else
            System.out.println ("Found "+search);
         


#4
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
owkay.... i understand... but with the code:

for (int k=b; k<array.length; k++)
                array[k]=array[k+1];
                array.length--;
                }

i just want to decrease the number of elements by 1 after deleting one of the elements...
how can i do that???

#5
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
You can't :D

private static final int SIZE=100;
int count =0;
int [] array = new int[SIZE];

...

//adds an element at the end of the array
private int addElement(int element){
   if(count<SIZE){
      array[count]=element;
      count++;
   }
}


//removes the last element of the array
private void removeLastElement(int element){
   if(count>0)
      count--;
}


//prints the array
private void printArray(){

   for(int i=0;i<count;i++){
      System.out.print(array[i]+" ");
   }

}

Note that this way you are only able to delete the last element of the array or add an element at the end of the array.
If you want to be able to add elements in every position of the array look up my reply to your other thread :)

p.s. note that you don't really delete array elements, you just use the variable count to know what's the last element of the array you should consider. It's a sort of length, but managed by you!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users