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 3 of 3

Thread: Understanding array operators

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Understanding array operators

    Hello!

    I am having trouble understanding how thing work with arrays specificly what the "=" operator represents. I've written a really simple code and I'm trying to fully understand what is happening, I am having a few doubts and some help would be appreciated. Thank you!

     
    public class test {
    	public static void main(String[]args) {
     
    		int[] numbers = {1,2,3,4,5};
     
    		int temp;
     
    		int start = 0;// first number in the array (1)
     
    		int end = numbers.length -1;//last number in the array(5)
     
    		while(start < end) {// keep going until the numbers "collide"
    			temp = numbers[start];//temp now holds the value of start(1)
     
    			numbers[start] = numbers[end];// this is the part that confuses me
     
    			numbers[end] = temp;//this as well
     
    			start = start + 1;// increase the value of start by 1
    		    end = end -1;// decrease the value of end by 1
     
    		}
     
    		Out.println(java.util.Arrays.toString(numbers));
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Understanding array operators

    Think of an array as a line of buckets that can only hold one type of thing, say fruit and only one at a time.
    If there are 5 buckets in front of you and from left to right (indexes 0 to 4)
    and the buckets contain an apple, orange, pear, peach and banana.
    What is in the bucket with index = 1? (an orange)
    numbers[start] = numbers[end];// this is the part that confuses me
    What if you copy the contents of bucket 3 (peach) to bucket 2? Now there are two buckets with peaches. The pear was replaced by a peach.
    Replace the specific numbers 3 and 2 with the contents of variables like end (=3) and start(=2).

    Work through the program's steps with paper and pencil to see what it does.
    If you don't understand my answer, don't ignore it, ask a question.

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

    arhzz (September 16th, 2020)

  4. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Understanding array operators

    Great answer thank you!

Similar Threads

  1. Splitting operators
    By zzQwerty in forum Object Oriented Programming
    Replies: 3
    Last Post: March 7th, 2018, 08:52 PM
  2. on unary operators
    By tappetivamsi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 28th, 2013, 08:12 AM
  3. Operators
    By Skybear in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 11th, 2012, 01:53 PM
  4. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 06:26 PM
  5. Need Help with Operators/ logic
    By codekiller in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2010, 09:25 AM