0

I am trying to write a little program that will contain a array of profiles of people and I am stuck on the method for adding the profiles, as I would like them to be added in correct place so it doesn't need to be sorted. For example If I have a array with 3 profiles

Potter, H Smith, T Warren, B

And I want to add Summer, P I would like it to be added right between the 1st and 2nd index

Before anyone asks I haven't got much code for this as I am still thinking on how to search the array and say where the profile needs to be placed.

Any ideas are appreciated

(Also it needs to be a Array not a ArrayList or any other data structure)

1
  • 1
    Why does it need to be an array? is this a real requirement in a project, or some academic project? Commented Apr 20, 2013 at 17:10

4 Answers 4

1

If you want to use an array rather than a decent, appropriate data structure, then use Arrays.binarySearch() to find the appropriate location. But you'll have to shift all the subsequent elements.

Sign up to request clarification or add additional context in comments.

2 Comments

OK Thanks, I understand that the elements need to be shifted but how would the code look like for the Arrays.binarySearch?
Have you read the documentation? What don't you understand in it? Take some time to read and understand it, to try something, and to fix the errors if you have any.
1

Whatever you are talking about is best done by the LinkedList http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html
Since you want to use Array only, then as you know arrays have a constant number of elements that you declare. So I recommend you to create a temporary ArrayList and then copy those elements into an array that you want. Here how it's done

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {
        String[] yourInitialArray = { "Potter, H", "Smith, T", "Warren, B" };

        // Creating a temporary ArrayList
        ArrayList<String> temporary = new ArrayList<String>();
        for (int i = 0; i < yourInitialArray.length; i++) {
            if (i != 1) {
                temporary.add(yourInitialArray[i]);
            } else {
                temporary.add("Summer, P");
                temporary.add(yourInitialArray[i]);
            }
        }
        yourInitialArray = new String[temporary.size()];
        for (int j = 0; j < temporary.size(); j++) {
            yourInitialArray[j] = temporary.get(j);
            System.out.println(yourInitialArray[j]);
        }

    }
}

1 Comment

@tomoosss123 please take a look at the above code. You may copy-paste it to Eclipse. Is that what you were talking about?
0

try to adding normally after that sort the list.It is better to use

Comments

0

First of all, I would highly recommend using the Collections framework List over the Arrays. because it provides the lot of flexibility and improvements over using normal arrays

and for your solution, i would recommend using the LinkedList. This provides the method add(int index, E element ) for inserting the element at specific location and it is very efficient

4 Comments

LinkedList is not very efficient at all. ArrayList is much more efficient in general. But a TreeSet looks more appropriate anyway.
@JBNizet how come TreeSet? As he didnot specify any criteria for Sorting right ? and LinkedList is more memory efficient for inserting an element at any ramdom position rather in ArrayList, that requires traversal across elements
The OP says: "I would like them to be added in correct place so it doesn't need to be sorted". So he wants an always sorted data structure. And ArrayList doesn't need traversal, since it's backed by an array. It needs shifting of its elements. Linked list needs traversal, since it only has a reference to the first and last nodes. It doesn't need shifting though. And it uses more memory that an array (although that has not much to do with efficiency).
@JBNizet yes, got it. Thanks! Me needs refreshment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.