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: Could someone help break this code down for me so i can understand it more?

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Could someone help break this code down for me so i can understand it more?

    I've only just started Java programming and I'm finding it difficult to understand the delete function here. Could someone please break it down for me so its easier to understand? Thanks.


    public static void main(String[] args)
        {
            char[] source = {'H', 'A', 'P', 'P', 'Y', 'L', 'E', 'A', 'R', 'N', 'I', 'N', 'G'};
            char[] destination = new char [7];
     
            System.arraycopy(source, 0, destination, 1, 5);
            System.out.println(new String(destination));
     
            //deleting element from array
            int flag = 3; 
            for (int i = 0; i < source.length; i++) { 
     
     
            //DELETE FUNCTION 
     
            if (flag == i){
            for(int j = i + 1; i < source.length - 1; j++){   
            source[i] = source[j];  
            i++; //
            }
            System.out.println(source);
    Last edited by soggla92; April 24th, 2020 at 12:25 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: Could someone help break this code down for me so i can understand it more?

    What part of the code are you having problems with?
    What happens when you compile and execute the code?
    Copy the program's output and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Could someone help break this code down for me so i can understand it more?

    Ah thanks, was unsure on how to do that.

    okay flag = 3 represents the letter P, and i understand how the for loop works below that, eventually the Value of I increases to 3 and when that happens the delete function is then triggered.

    if flag == 3, then int j will = 3+1 = 4 if i (3) is less than length (12) then it will add 1 onto the value of J which will then be 4 right?

    it will repeat this process until i is no longer less than 12? which will then move onto the next part of the code
    source[i] = source[j]?

    At this point i = 11 and j =12 ?


    The outcome is HAPYLEARNING but i want to know whereabouts in the code the letter P is deleted, if that makes sense. Sorry im a complete newbie.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: Could someone help break this code down for me so i can understand it more?

    i want to know whereabouts in the code the letter P is deleted,
            source[i] = source[j];    // copy a char to the left from index=j to i
    That statement is inside a loop that starts when i is at the location of the char to be deleted.

    To see what is happening print out the value of source after the above assignment statement.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soggla92 (April 24th, 2020)

  6. #5
    Junior Member
    Join Date
    Apr 2020
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Could someone help break this code down for me so i can understand it more?

    Thank you! I understand it alot better now

    --- Update ---

    Hi, just a quick follow up, the index of J isn't increased by 1 until the following code has been executed right?:

    source[i] = source[j];

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,110
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: Could someone help break this code down for me so i can understand it more?

    [code] for(int j = i + 1; i < source.length - 1; j++){ ]/code]
    j is the for loop variable. It would be incremented at the end of the loop, before the next iteration.
    If you don't understand my answer, don't ignore it, ask a question.

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

    soggla92 (April 24th, 2020)

Similar Threads

  1. Can't understand where should I use longpress in my code.
    By fallen01 in forum Android Development
    Replies: 10
    Last Post: January 26th, 2019, 02:36 PM
  2. I dont understand this code ?
    By ammehmeti in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 22nd, 2014, 07:57 AM
  3. Help me understand this code?
    By Farkuldi in forum Java Theory & Questions
    Replies: 11
    Last Post: November 12th, 2013, 03:37 PM
  4. Java HSA Break To Part of Code
    By nigerianmahogany in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 30th, 2013, 11:20 PM
  5. Hi,Can any one help me to understand this code.Am a beginner.
    By Jawad24 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 23rd, 2013, 08:47 AM

Tags for this Thread