I am trying to write a searching algorithm to check one node in a linked integer list vs another node in that same list.
I think there seems to be a problem with the IF statement (data is in the list):
LinkedList<Integer> listScore = new LinkedList<Integer>();
int temp = 0;
String temp2 = "";
boolean flag = true;
while (flag){
flag = false;
for (int j = 0; j < linebr; j++){
if (listScore(j) < listScore(j+1)){
System.out.println("Testing");
}
}
I did originally try listScore[j] but I think there is something wrong with the way I have written it. Any help or explanation as to why it doesent work would be Greatly appreciated!!
Instead of listscore(j) and listscore(j+1), what you need to get values inside LinkedLists is .get(). So you should be using listscore.get(j) and listscore.get(j+1).
Related
I would like to know how array[array[i]]++ works in java.
I wrote the code, and want to know how this count integer array is working here
int[] counts = new int[201];
for (int i = 0; i < d; i++) {
counts[array_inside[i]]++;
}
and
also would like to know if i does like below how count array values will be written and left or right shift its values
for(int i = j; i < array_inside.length; i++){
count[array_inside[i-j]]--;
count[array_inside[i]]++;
}
Consider it as two operations (because it is). This
counts[array_inside[i]]++;
is equivalent to
int p = array_inside[i];
counts[p]++;
I have a JLabel array that starts with an integer number of elements. How can I remove an certain number of elements from the array? For example, every time the int is updated:
int i = 21;
i = i - removedElements
How can I update the array to contain that many elements, instead of creating an entirely new array with the desired number of elements?
As others have already mentioned, List is the way to go here since it is specifically designed for adding and or deleting elements.
However if you would prefer to use the JLabel Array you already have in established then you will need to realize that the only way to delete an element from that array is to actually create another array with the desired element to delete excluded from it then return it into the original array. Below I have supplied a simple method named deleteJLabelFromArray() that can do this for you:
public static JLabel[] deleteJLabelFromArray(JLabel[] srcArray, int... indexesToDelete) {
int counter = 0;
JLabel[] newArray = new JLabel[srcArray.length - indexesToDelete.length];
for (int i = 0; i < srcArray.length; i++) {
boolean noGo = false;
for (int j = 0; j < indexesToDelete.length; j++) {
if (i == indexesToDelete[j]) { noGo = true; break; }
}
if (noGo == false) { newArray[counter] = srcArray[i]; counter++; }
}
return newArray;
}
With this method you can delete whatever indexes you supply within the indexesToDelete argument (delimited with a comma). Copy/Paste the code into your project then you can use it something like this:
JLabel[] jla = {jLabel2,jLabel3,jLabel4,jLabel5};
jla = deleteJLabelFromArray(jla, 2);
for (int i = 0; i < jla.length; i++) {
System.out.println(jla[i]);
}
In this example we are going to delete the element number 2 (remember that arrays are 0 based) and therefore jLabel4 would be removed from the Array.
Keep in mind that this would be scary stuff with really big arrays.
Hope this helps.
I'm working on a homework assignment right now. I am supposed to set up a deque which is easy enough, but I'm also supposed to delete from the front, have the array act like a deque.
My problem is that for whatever reason it's not working. Here's the method I created to delete from the front:
public Object deleteFromFront(Object e)
{
Object[] temp = new Object[capacity];
for(int i = 1; i < size() - 1; i++){
temp[i] = A[i+1];
}
for(int i = 0; i < size() - 1; i++)
{
A[i] = temp[i];
}
A = temp;
return A;
}
The thinking is that I create a temp array in which the other array will be moved into it starting at position 1. As such, it will ignore the first one, thus starting with the second one. However, whenever I do this, it doesn't seem to be deleting the first. Can anyone tell me what I'm missing? Thanks a lot!
Your solution doesn't seem right.
What's the use of Object e?
What is A? Is it an array of Object type? Is is declared in the method?
Im trying to make every "ADJEKTIVER" strings in the storyList array into random strings from my adjectivesList array. When I'm trying to compile, I only get the error: cannot find symbol- variable. With .length as the variable/attribute. Been searching for a solution quite a while now
ArrayList<String> storyList = new ArrayList<String>();
storyList = reader.getWordsInFile(storyFilename);
ArrayList<String> adjectivesList = new ArrayList<String>();
adjectivesList = reader.getWordsInFile(adjectivesFilename);
for (int index =0; index < storyList.length; index++)
{
storyList[index] = storyList[index].replace("ADJEKTIV", adjectivesList[random.nextInt(adjectivesList)]);
}
writer.write(adjectivesList, outputFilename);
use storyList.size() method instead of storyList.length
for (int index =0; index < storyList.size(); index++)
Arralist or List doesnt have any property named "length". use size() method instead
Arrays have a public final int property called length, that holds the number of elements in the array, arrays can't change how many elements they have in Java.
Which brings me to ArrayList having a collection that can change the number of elements it has is useful, but it's not magic(I would've liked someone to tell me this). It has an Array of type E[] I will call this internal array arr.
When you push an element to an ArrarList
public boolean add(E e) {
E[] temp = new E[this.arr.length];
for (int i = 0; i < this.arr.length; i++)
temp[i] = this.arr[i];
this.arr = new E[this.arr.length + 1];
for (int i = 0; i < this.arr.length; i++)
this.arr[i] = (i == temp.length ? e : temp[i]);
temp = null; // free temp or let it fall out of scope
return true;
}
// Removing is easy too
public E remove(int index) { // I'm just doing the first of the overloaded methods, in api
E returnValue = this.arr[index];
E[] temp = new E[this.arr.length - 1];
for (int i = 0; i < this.arr.length; i++)
if (i != index) temp[i] = this.arr[i];
this.arr = new E[this.arr.length - 1];
for (int i = 0; i < this.arr.length; i++)
this.arr[i] = (i == temp.length ? e : temp[i]);
temp = null; // free temp or let it fall out of scope
return returnValue;
}
The being able to change the length forces it to be accessed in a dynamic way in Java via a method called size in this case.
public int size() {
return this.arr.length;
}
Java exists both as a standard and as an implementation, the former might be implemented such that Strings are mutable so making String have a length method makes code more compatible between the implementations of the standard, as implementations(not necessarily those of Java) have been known to deviate, so making them more compatible makes sense for the Java devs.
As for why ArrayList uses size() as the method name, while String uses length(), and Array uses length, I don't know although in my opinion(warning opinions ahead) it's not unreasonable that accessing the lengths got implemented the way they did, and the Java devs felt locked in, because changing the spec would also required changing the code in question.
public int length() {
return this.arr.length;
}
All code shown is merely for demonstration purposes, and comes as is, and is without warranty of any kind.
I need a method to delete a determinate element from a handmade list.
I have a class called AudioFile. Each element of the list is an object from this class.
Then, I have the class FileTable, with this atributes:
private AudioFile[] table;
private int size;
I defined this method, but It doesn't work when I call it:
public void deleteFile(AudioFile file){
AudioFile[] table2 = new AudioFile[100];
int j = 0;
for (int i = 0; i < size; i++){
if (table[i] != file){
table2[j] = table[i];
j++;
} else {
i++;
}
}
for (int k = 0; k < size); k++){
table2[k] = table[k];
}
table[size-1] = null;
}
I think the code it's easy to understand, but if you don't understand something or you need some more information about the class AudioFile, you can say it and I'll try to help too. Thank you!
You're reinventing the wheel, just use a Collection like a List or a Set. They have already a remove() method that does exactly what you want to do.
See Arrays as a low level type and always use Collection (except in really specific cases).