This question already has answers here:
Default or initial value for a java enum array
(5 answers)
Can we assume default array values in Java? for example, assume that an int array is set to all zeros?
(4 answers)
Closed 4 years ago.
I created an enum array like this:
enum MyEnums {
FIRST, SECOND, THIRD, FOURTH;
}
public class MyEnumsTest {
public static void main(String[] args) throws Exception {
MyEnums[] myEnums = new MyEnums[4];
for(int i = 0; i< myEnums.length; i++) {
System.out.println(myEnums[i]);
}
}
}
But why is the output null, null, null and null? And how can I get the element by myEnums[i].FIRST?
What you're doing here is creating an array of MyEnums, and the default value is null (you haven't set the values in the array).
If you wanted to print out the enum values you can use the values() method:
for(MyEnums en : MyEnums.values()) {
System.out.println(en);
}
or (more like your original code)
for(int i = 0; i < MyEnums.values().length; i++) {
System.out.println(MyEnums.values()[i]);
}
This prints:
FIRST
SECOND
THIRD
FOURTH
Related
This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 2 years ago.
I want to be able to check if the string input matches any of the strings in array, and then run only if it matches any of the strings.
String[] list = {"hey","hello"};
if (input == anyofthestringsinarray) {
}
Thought something like if(input.equalsIgnoreCase(list)) {} could work, but dont. Any tips?
Try this:
public static void stringContainsItemFromList(String inputStr, String[] items)
{
for(int i =0; i < items.length; i++)
{
if(inputStr.contains(items[i]))
{
// if present do something
}
}
}
This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 3 years ago.
I'm trying to make a program in which an object is declared every time a loop is passed through.
package sequence;
public class SequenceServer {
SequenceObj sequence = new SequenceObj();
public void findSequence(int[] sequence, int lastNumber) {
for(int i = 0; i < sequence.length; i++) {
SequenceObj sequenceTemp = new SequenceObj(); // this line is wrong, but I don't know how to make it work
}
}
}
There are no errors, but I need to make it so a potentially infinite number of variables can be declared through the user interacting with the program, as opposed to me individually declaring all of the variables.
Try this:
`public class SequenceServer {
SequenceObj sequence;
public void findSequence(int[] sequence, int lastNumber) {
for(int i = 0; i < sequence.length; i++) {
sequence = new SequenceObj(); // this line is wrong,
// but I don't know how to make it work
}
}
}`
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
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.
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.
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
This question already has answers here:
Why am I not getting a java.util.ConcurrentModificationException in this example?
(10 answers)
Closed 7 years ago.
I am trying to add "Luis" 3 times to array list and then remove "Luis" so there is only one "Luis". Seems to be a problem with the if.
import java.util.ArrayList;
public class Menu {
private ArrayList<String> meals;
public Menu() {
this.meals = new ArrayList<String>();
}
// Implement the methods here
public void addMeals() {
this.meals.add("Luis");
this.meals.add("Luis");
this.meals.add("Luis");
for (String container : this.meals) {
for (int counter = 0; counter < this.meals.size(); counter++) {
***if (counter > 1 && this.meals.contains(container)) {
this.meals.remove(this.meals.indexOf(container));
}***
}
}
System.out.println(this.meals);
}
}
An ArrayList can contain duplicates. There are other Java collection classes which can only contain unique elements, such as Set.
I'd suggest you look at the documentation for Set and its implementations, this would likely solve your issue.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am trying to compare an String and an array element. If the requested element
package com.company;
public class Main {
static String[] List = {
"EUR", "AED"
};
static String[] IdList = {
"EUREUR", "EURAED", "AEDEUR","AEDAED"
};
public static void main(String[] args)
{
String value1 = "EUR";
String value2 = "EUR";
for(int i = 0; i < IdList.length; i++)
{
System.out.println(value1+value2 == IdList[i]);
}
}
}
The problem is that it always returns false . Even if the requested String matches to a value in the array. Can you help me?
You must use String.equals(), not the == operator, to compare strings reliably.