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: indexOf Linked List

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default indexOf Linked List

    I'm doing the following problem: Write a Java program to find the index of a value in a sorted array. If the value does not find return the index where it would be if it were inserted in order.
    My issue here is when I add a value (not already within the list - and after sorting it), I don't get the index of this value. It keeps returning me -1, I do not understand why? what is wrong? Or what am I missing? Any explanations? Help? Thank you

    Here is my code :

    import java.util.*;
     
    public class indexSortedArray {
        public static void main(String[] args) {
     
            try (Scanner input = new Scanner(System.in)) {
                // init array and linked list of array nums
                Integer[] nums = { 1, 2, 4, 5, 6 };
                List<Integer> list = Arrays.asList(nums);
                java.util.LinkedList<Integer> linkedList = new java.util.LinkedList<Integer>(list);
     
                // get user input value
                System.out.print("Value you are looking for? ");
                int value = input.nextInt();
     
                // if value not member of list, add value and sort the list
                if (isMemberLinkedList(linkedList, value) == false) {
                    linkedList.add(value);
                    Collections.sort(linkedList);
                }
     
                System.out.println("LinkedList of above array "
                        + linkedList); // print linked list
     
                // get value index and print it
                int getIndex = Arrays.asList(nums).indexOf(value);
                System.out.println(value + " is located at " + getIndex + " index"); // -1 if not inside the list
            }
     
        }
     
        // check if value inside linked list
        static boolean isMemberLinkedList(List<?> list, int val) {
            boolean check = false;
            Iterator<Integer> it = (Iterator<Integer>) list.iterator(); // to iterate through the linked list
            // browse linked list
            while (it.hasNext()) {
                if (it.next() == val) {
                    check = true;
                    // System.out.println("change made");
                }
            }
            return check;
        }
    }

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

    Default Re: indexOf Linked List

    I don't get the index of this value.
    Can you post the programs output that shows what you are talking about?
    What list does the program search? Is that the list with the added number?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: indexOf Linked List

    On those following lines, I initialize an array and convert it to a list ait Arrays.asList()
    From that list, I search for my value within the list and give the index if the value given by the user is already within the list.
    If not, we add the value to the list, then sort it, and then give the index.

                Integer[] nums = { 1, 2, 4, 5, 6 };
                List<Integer> list = Arrays.asList(nums);
                java.util.LinkedList<Integer> linkedList = new java.util.LinkedList<Integer>(list);


    Concerning the output, here is it for the case when the value is not already a member of the list.

    Value you are looking for? 10
    LinkedList of above array [1, 2, 4, 5, 6, 10]
    10 is located at -1 index

    As you can see, it gives me -1 as the index of 10 which is not what I want. I believe, it should give me 5 instead of -1.

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

    Default Re: indexOf Linked List

    add the value to the list, then sort it, and then give the index.
    What is the variable name of the list with the added value?
    What is the variable name of the list that is searched?
    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:

    siid14 (May 31st, 2022)

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

    Default Re: indexOf Linked List

    @zemiak
    Please let the OP find the error. My suggestions should help. Giving the OP the answer does not help the the OP work through the debugging process needed to find errors.
    If you don't understand my answer, don't ignore it, ask a question.

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

    siid14 (May 31st, 2022)

  8. #6
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: indexOf Linked List

    Thank you, I just realized the error.
    I was getting the index from the previous list (original list) instead of the new sorted list - silly mistake...

Similar Threads

  1. Replies: 1
    Last Post: January 12th, 2021, 06:38 AM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  3. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  4. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM
  5. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM

Tags for this Thread