0

Implement a method for adding elements to the class CacheMemory. The Class cache memory has an array memory whose length is passed through a constructor.Elements can be added to the array only if it has not been added before and if the length of the arrays added is within the boundaries of the array.(within its length).

This is the code I came up with so far:

public class CacheMemory {

    private String[] memory;

    public CacheMemory(int length) {
        this.memory = new String[length];
    }

    public void addingElementsToCache(String mem) {
        for (int i = 0; i < memory.length; i++) {
            if (memory[i] != mem) {
                memory[i] = mem;
                System.out.println(mem);
                break;
            } else {
                System.out.println("Element already exists");
            }
        }
    }
}

If i call this method without break,of course it will print out the string five times,but I don't want the same string to be printed out five times,I want to add five different strings and then,while loop goes through the array,and comes to element that has already been passed,to print out the message.

Suraj Bajaj
  • 6,630
  • 5
  • 34
  • 49
Temp034
  • 141
  • 11
  • `if (memory[i] != mem) {` should be `if (memory[i].equals(mem)) {` since you compare strings – XtremeBaumer Nov 30 '16 at 14:39
  • You are using `!=` to compare strings. That does not do what you think it does. Use `!memory[i].equals(mem)` instead. – Jesper Nov 30 '16 at 14:40
  • 6
    See: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java/513839) – Jesper Nov 30 '16 at 14:40
  • This is more like a code review than an actual precise question. – Christophe Roussy Nov 30 '16 at 14:45
  • How exactly is the problem supposed to be solved? Like more specifics than a unique array... What if the array is already full? Also @XtremeBaumer that's a NullPointerException since `memory[i]` starts as null – OneCricketeer Nov 30 '16 at 14:48
  • It turned out to be a review,actually,I just wanted to offer my idea. If someone knows a better way to implement this method I would like to see it. – Temp034 Nov 30 '16 at 14:52

2 Answers2

0

Actually , you need to use !string.equals("anotherString") instead of !=,since the != only compare the address of the string ,instead of the content of the string,but the method equals does it.

0

You got some of the logic wrong. You have to wait until you have checked all elements in the cache before you can decide that it doesn't already exist. And also, you should use .equals() for comparing Strings.

public void addingElementsToCache(String mem)
{
    // Loop over slots
    for (int i = 0; i < memory.length; i++)
    {
        if (memory[i] == null) {
            // You have reached an unused slot, use it!
            memory[i] = mem;
            System.out.println(mem);
            return;
        }
        else if (mem.equals(memory[i])) {
            // You found a duplicate
            System.out.println("Element already exists");
            return;
        }
    }
    // You have checked all positions without finding an empty slot
    System.out.print("The cache was full, unable to add!");
}

If you exercise this code with

public static void main(String[] args)
{
    CacheMemory cache = new CacheMemory(10);
    asList("foo", "foo", "bar", "boz", "bar")
         .forEach(cache::addingElementsToCache);
}

... it will print the following, which is what I think you expect:

foo
Element already exists
bar
boz
Element already exists
Per Huss
  • 4,755
  • 12
  • 29
  • Thank you Per,it worked! I already passed the array value through the constructor so it is printing as expected. – Temp034 Nov 30 '16 at 15:56