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: array searching the location in one line

  1. #1
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default array searching the location in one line

    I need to solve the the problem in a simple way. kindly tell me how i can print the location of multiple data found in a single line. Suppose there is 100 time value found of 8 then i should print all the locations in a single line.



    [img]undefined[/img]



     
    package com.company;
    import java.util.Arrays;
    import java.util.List;
    import java.util.ArrayList;
    public class Main {
        public static void main(String[] args) {
            //Initialize array
            int [] arr = new int [] {1, 2, 1, 4, 2, 1, 8, 8, 3};
            int[] location=new int[10];
            System.out.println("Duplicate elements in given array: ");
            //Searches for duplicate element
            for(int i = 0; i < arr.length; i++) {
                for(int j = i + 1; j < arr.length; j++) {
                    if(arr[i] == arr[j])
                    {
                        System.out.print("value found    "+arr[i]+" in multiple positions at location no :");
                        System.out.print(i +" "+j+" ");
                        System.out.println( );
                    }
                }
            }
        }
    }

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

    Default Re: array searching the location in one line

    print all the locations in a single line.
    To print multiple items on a single line, do not include line-end characters between the items. Have a single line-end character at the end of the line.

    Please do not post images. Copy any text to be displayed and paste it as part of the thread.
    Text shown in an image can not be copied into a response.
    Also some font sizes and colors can be hard to read.

    Can you post an example of the desired output?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: array searching the location in one line

    Quote Originally Posted by Norm View Post
    To print multiple items on a single line, do not include line-end characters between the items. Have a single line-end character at the end of the line.

    Please do not post images. Copy any text to be displayed and paste it as part of the thread.
    Text shown in an image can not be copied into a response.
    Also some font sizes and colors can be hard to read.

    Can you post an example of the desired output?

    the value 1 found in multiple positions : 0 2 5
    the value 2 found in multiple positions : 1 4
    the value 8 found in multiple positions : 6 7


    and without giving image it's not possible for anyone what output i want.

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

    Default Re: array searching the location in one line

    the value 1 found in multiple positions : 0 2 5
    The first part would be printed first, then the part in red would be printed later inside of the search loop.
    Perhaps a counter or boolean value could be used to control the printing of the first part only one time.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2021
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: array searching the location in one line

    it's not an easy task for me .. i need to solution or code. giving suggestion is so easy what i can also give that .. if you can code it.

    an array contains an int number of 100+. int[] array={1,2,9,9,1,1,1,9,1,9,2,9,0,0,8,1,8,0.......} ; now i need to print the value of similar elements location . output like this the value 1 found in location at 0 4 5 6 8.... and the value 2 found in location 1 10 ......



  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: array searching the location in one line

    Can you describe in English what steps the program needs to take to solve your problem?
    Once you get a list of the steps needed, then work on writing the code to implement the steps.

    The posted code is a start. It just needs some controls for when it prints the first part of the line and when it prints the rest of the line.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Need help displaying the location of array index
    By PCn3rd51 in forum What's Wrong With My Code?
    Replies: 22
    Last Post: February 13th, 2014, 11:15 PM
  2. Searching a 2d Array for lines of 3
    By Lurie1 in forum Loops & Control Statements
    Replies: 4
    Last Post: January 20th, 2014, 02:00 PM
  3. [SOLVED] Won't display its Location for Linear Searching.
    By Evilreaper in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 24th, 2013, 10:41 AM
  4. first empty location in an array!?
    By keep smiling in forum Collections and Generics
    Replies: 2
    Last Post: February 10th, 2012, 07:29 AM
  5. Please help i cant figure it out - problems with Location and Line
    By tuts73 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 19th, 2012, 08:58 PM