How to check an array before populating it [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Does anyone know how can I populate an array that checks if the positions are filled before filling them, and if they are already filled and if that array position is filled it increments to the next one.
Thanks

Primitive array elements are never empty. For example in an int array all elements will be initialized to 0.
So if you want to check for filled or not, initialize the array elements to a value which it is not going to take. say -1. Then each time you make an entry check for -1. The dummy value initialization is necessary as you can not check for 0, because 0 can be a valid data as well
// Initialization part
int [] arr= new int [17];
for(int i=0;i<arr.length;i++)
{
arr[i]= -1;
}
Hope you will do the checking part yourself

I am assuming (possibly incorrectly) that you either a) want a method that inserts object into the first available (i.e. not null) spot in the array at or past the argument integer or b) want to insert repeatedly into the array using the method in a)
the method in a) would basically be as follows
public <Type> void myInsertMethod(Type[] array,Type item,int position)
{
while (array[pos]!=null)
{pos++;}
array[pos]=item;
}
if you want to do this repeatedly on the same array, just call the method repeatedly, with different positions and items.

You can check if the position you are going to fill it's not null, or empty, or at least the stored value it's different to the one you are going to put
if(array.get(index) != "" && array.get(index) != null) && array.get(index) != newObject){
array.add(newObject);
}
Then, depending on the stored value type, you can ddo some more checks, for example in the cas e of the int, as RookieB says, you can check if the object is different to 0
Hope it helps

Related

two arrays are always equal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this code
for (int i=0; i<tini.length; i++){
tini[i].tempLabel.setText("Temp: "+ Float.toString(tempArray[i]) +"°" );
out_status[i] = tini[i].alarm;
frame.statusLabel.setText("Connetction: OK, String: OK");
}
System.out.println("old: " + Arrays.toString(out_status_old));
System.out.println("new: " + Arrays.toString(out_status));
if (Arrays.equals(out_status, out_status_old) ){
System.out.println("UGUALI");
}
out_status_old = out_status;
the resulting arrays are always equal. I cannot understand the reason.
Using a Button in JFrame, in a GUI interface i can modify the value of alarm, but both the old value and the actual one change at the same time!
When you access and then update the elements of one array, you're also updating the elements of the other array because they are referencing the same objects. You need to create separate items within each array while populating the arrays.
You left out an important part of your program which is where you're actually populating these arrays. Odds are, you are not doing a deep copy.
Deep copy of an object array
The line out_status_old = out_status; does not create a copy of the array. You have just two variables, out_status and out_status_old, pointing to the same array.
If you want to create a proper copy of the array, you can e.g. use Arrays.copyOf (or one of its variants).

Can we create our own stack data structure implementation with out array or List in java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I had been asked a question in interview as below.
Can we implement our own Stack Data structure without using arrays or lists or even node types.
Is it possible?
You can use the call stack, but that wouldn't be useful in most scenarios.
Suppose you have an interactive application in which the user can choose to push or pop elements.
You'll have a method with a single local variable that holds the top element of the stack.
That method will ask the user to choose whether to pop the last element or push a new element.
If the user chooses push, the method will make a recursive call to itself, passing the pushed element as argument (which would be stored in the local variable of the called method).
If the user chooses pop, the method will return its local variable to the calling method.
Here's some pseudo code :
public static Object stack (Object element)
{
Object top = element;
int input = 0;
while (input != 2) {
input = ... // get user input - 1 for push 2 for pop
if (input == 1) {
Object newElement = ... // get input from user
Object poppedElement = stack (newElement); // push the new element
}
}
return top; // pop the top of the stack
}
Well, if you cannot use any collection or array, you can use a file, and each line of that file can be an element pushed to the stack (the file), And giving the principle of the stack LIFO, than the first line of the text file would need to be retrieve at pop, or the pushed element will be added in the first line of the text file.

Why indexOf doesn't work as expected? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have labels which is a list of String and is equal to [0,1]. However the following line returns -1 while I expect it to return 1. Any idea what could be wrong or missing?
definition:
maxLabel is an integer initially set to -1.
public List labels;
where the unexpected result happens, while maxLabel is equal to 1:
int maxLabelIndex=labels.indexOf(maxLabel);
indexOf returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null
In your case the index of element "1" is 1, thats why you will get 1. BUT if you will pass int maxLabel = 1; notice that maxLabel is declared as type int then you will get -1 because Collection of strings doesn't contains an object of type Integer.
Basically indexOf method take as parameter o of type Object. Whenever you pass a different Object type (e.g. you declared your List of type String and you are checking for different types like Integer,Double,int,long, etc.), you can expect to get -1

Remove elements from arraylist based on a condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an ArrayList whose each element is of type DataType, where DataType is a class:
class DataType{
String dId;
String dType;
String rId;
}
I need to remove all such elements from the list whose rId is equal to any other element's dID.
i.e. if DataType D1 has value of dID as "abc" and DataType D2 has value of rID as "abc", than remove both D1 and D2 from the list.
Could someone please suggest the most appropriate approach for doing this.
The easiest would be to traverse the list once and create a HashMap<String, List<DataType>>.
You will map every object to their dID which forms the primary key.
After that you can iterate over your ArrayList, check the rId of the current object and see if it's in the HashMap. HashMap has O(1) lookup time so this should be a non issue. If the value is present, remove the current value (you're using an Iterator to prevent a ConcurrentModificationException) and remove the objects inside the value-part of the key-value pair as well.
Make sure you have correctly implemented .equals(Object o) and .hashcode().

Adding up values of items in an ArrayList [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
ive hit a road block and im stuck with a question on my work.
Here is the question:
Provide an implementation of the getLoad method that adds up the individual weights of
the items in the items list and returns the total.
The items list is:
ArrayList<Item> items;
Ive done what i thought was right but for some reason its not working.
Any help on what is wrong, or if what im doing is wrong? thanks
#Override
public int getLoad() {
int load = 0; //declare the variable
for (Item i : items) { // for each item in the list of items
load = load + i.getWeight() ; // load equals the weight of the item and adds on
}
return load; //returns it
}
The one thing I could see going wrong is if getWeight() returns a double. In that case, you should make int load: double load instead
With the limited information you've provided here, I can only take a wild guess that the problem is either:
The list items is empty even though it shouldn't be. Make sure that Items actually get added to the list! Use a debugger or a printed message to find out if the item list is empty on getLoad().
Weights for each Item are not assigned correctly, so getWeight() returns zero for each of them. Make sure that each Item added to the list actually gets assigned its proper weight.
Posting more code would help us give a better answer.

Categories

Resources