0

I'm starting with java, and I need to create a method for my first program.

What I have to do is this: I create Strings one by one, and I save them on a String[] array. These strings have this structure:

String = word1 word2 word3 code;

Then, when I want, I can list them showing the entire array on the console. And also, if I want, I can delete one specific string from the array.

To achieve this, I'm using the code that I asign to each array. This code is generated each time I add one String to the array, so this code matches with the position that the string has in the array.

When I want to delete one String, I have to pass to the method the code of the string. In this case, the code is the same as the position of the string, so when I pass the code, actually what I'm passing is the position.

The first time I delete a String this works, but the second time fails because of this. Lets suppose we've got:

String0 = word1 word2 word3 0
String1 = word4 word5 word6 1
String2 = word7 word8 word9 2

If I delete String1, now the array is resized to 2 Strings (0 and 1), so when I add a new String, it will reasing the code 2 to this new String, so I will have:

String0 = word1 word2 word3 0    
String2 = word7 word8 word9 2
String3 = word10 word11 word12 2

This all is caused because to asign the code to the string, I use the position of the String in the array. So, what will the best way to have this functionality but in a correct way?

This is my code:

I add a string to the array this way in the main:

musicList.add(new Music(title, autor, format, code));

I list the array on the console this way:

System.out.println(musicList.toString());

And I delete it this way:

musicList.delete(code);

The methods add(), toString() and delete() are custom methods that I've defined in other class this way:

public void add(Music m){
    musicList[pos_counter] = m;
    pos_counter++;
}

public void delete(int code){
    Music[] tempMusic = new Music[musicList.length];
    int index_1 = 0;
    for (int i=0; i<musicList.length; i++){
        if(i != code){
            tempMusic[index_1] = musicList[i];
            index_1++;
        }
    }
    int index_2 = 0;
    for (int i=0; i<tempMusic.length; i++){
        musicList[index_2] = tempMusic[i];
        index_2++;
    }
    pos_counter--;
}

The toString() method simply shows the instance in a String way.

Thes Music[] instance just calls to other class where is defined a constructor with the elements of the Music List (these are title, autor, format or type and code):

public Music(String title, String autor, String type, int code){
    this.setTitle(title);
    this.setAutor(autor);
    this.setType(type);
    this.setCode(code);
}
8
  • What does 'code' represents in your logic? Commented Dec 3, 2013 at 10:52
  • Why don't you use a List (e.g. ArrayList) instead? It does all the internal array handling for you! Commented Dec 3, 2013 at 10:53
  • @Lahniep code is just a number that I have to asign to each string I add to the array, to be able to delete it by selecting it by the code Commented Dec 3, 2013 at 10:53
  • @isnot2bad i have to do it this way, is just the way is defined to me that i have to do it Commented Dec 3, 2013 at 10:54
  • OK. Rethink your delete method. If code is the index, it can be done much simpler! You don't need a temp array too. And only one simple for loop. Commented Dec 3, 2013 at 10:56

2 Answers 2

1

First of all, I would suggest using an ArrayList. However I have seen in your previous questions that you don't want to use it. So, if you want to use arrays, there is a problem with your code. When you delete something from the array, you will need to change the codes for all of the items in the array that are after the one you are deleting, or the code will not match the position.

EDIT: to do it with an ArrayList, you can create it with:

ArrayList<Music> musicList = new ArrayList<Music>();

And to add and delete items, simply:

musicList.add(new Music("string", "string", "string", musicList.size()));

// If you have the Music instance you want to remove, you can pass it as an argument
musicList.remove(musicItem);

// And if you only have the position, then
musicList.remove(position);

If you use an ArrayList, you won't need to store the position in the Music object, but if you need for some reason, then just use the size method I wrote as the last argument for the add function.

EDIT 2: to get a new code for each item you add to the list, you could try the following:

int max = 0;
for (Music item : musicList) {
    if (item.getCode() > max) {
        max = item.getCode();
    }
}
int newCode = max++;

EDIT 3: after everything you have explained, then I guess what you are really looking for is a Map. With a Map you can store items with a key, and retrieve values using this key. The key in your case would be the code of the Music item. This is what it would look like:

Map<Integer, Music> musicMap = new HashMap<Integer, Music>();

// To put a new item in the Map
int max = 0;
for (Integer mapCode : musicMap.keySet()) {
    if (mapCode > max) {
        max = mapCode;
    }
}

int newCode = max++;
musicItem.setCode(newCode);
musicMap.put(newCode, musicItem);

// To remove an item by its value
musicMap.remove(code);

You can find more information about the Map interface here.

LAST EDIT: to iterate through the map and print its output to the console, you can just do the following:

for (Music item : musicMap.values()) {
    System.out.println(item);
}
Sign up to request clarification or add additional context in comments.

13 Comments

Ok, but when adding new string, is not the size what i have to add, is a number wich represents the code of that string. And when i want to delete that, I have to pass this code this way: delete music with code X so even though using arraylist is a better way, using the position as the code generates the same problem i have now. Or maybe with the arraylist, there is some way to reasing this code to the strings each time i delete one
Well, the code number is just a number I have to asing to a string each time i add it to the array, so this way i could say that string_1 has de code "1", or string_5 has the code "23". This number is used to delete this especific string, or to add this string to another array. In my program, to make it easy i decided to set the position of the string in the array as the code number. but this wasn't a nice aproach because when you delete one string as I explain in the post, when adding a new string I used to get a duplicated code number cause of it's asigning the same position the previous had
Then each time you add a new item to the ArrayList, you could iterate through all the items in it to get the maximum code, add 1 to it and store it as the code for the new number
Could you edit your answer with this new approach? As I said i'm starting with this and I'm still a bit lost with ArrayLists
Done, that should do what you want
|
0

Consider using Java Collections

It already implements insertion, deletions and other nice operations for you. You may create a collection of 'Music' and operate on it.

1 Comment

How would you do it the way you say, considering the way I add the objects to the array, because I call to the constructor Music

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.