I am using arraydeque to store another arraydeque of type Integer.
My code looks like this:
private ArrayDeque<ArrayDeque<Integer> > grid;
public void initiateGrid(){
for (int i = 0; i < Length; i++)
{
ArrayDeque<Integer> columns = new ArrayDeque<Integer>();
for (int j = 0; j < Width; j++)
columns.add((int)(Math.random() * 100));
grid.add(columns);
}
}
When I try to run this it gives me a NullPointerException on grid.add(columns).
does anyone know what am I doing wrong?
Also if there is a better way to do this, please let me know.
Thanks
initialize grid, because you cannot use a null object, otherwise you get NullPointerException
public void initiateGrid(){
grid = new ArrayDeque<ArrayDeque<Integer>>();
//...
}
Related
I am trying to access an array from a method that returns an array within the same class. I then need to write back to the array for future use. Here is the code so far: ```
public int [] unitSelection(){
int [] selectedUnits = new int[numberCheck()];
return selectedUnits;
for (int i = 0; i < checkBoxList.length; i++) {
if (checkBoxList[i] == e.getSource()) {
index = i;
unitSelection()[i] = index;
export_to_SRF.setEnabled(true);
}
```
The code "unitSelection()[i]" is supposed to update the array in the "selectedUnits" Array in the "unitSelected" method. I have been reading but cant find anything in relation to referencing arrays of an external method. Thanks in advance
for (int i = 0; i < checkBoxList.length; i++) {
if (checkBoxList[i] == e.getSource()) {
index = i;
int[] x = unitSelection();
x[i] = index;
export_to_SRF.setEnabled(true);
}
But I don't think this will do what you want. unitSelection() returns a new array every time, so that won't actually update an array.
I try to implement a 2D Stack like this
private Stack<Char>[][] objectGrid = (Stack<Char>[][]) new Stack[width][height]
However, when I try to push an element to my stack, I keep getting NullPointerException
(objectGrid[x][y]).push(ch)
I checked the debugger, and figured that objectGrid[x][y] appears as null, so I can't do push on it. Is the above initialization wrong, should I do a for loop to initialize the second dimension of my stack array?
Add initialize loop to your code, like this:
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
objectGrid[i][j] = new Stack<Char>();
}
}
I'm reading and learning about a Java implementation of radix sort, as shown below. It would be great if someone could clarify the logical meaning of pointTo, index and globalPtr.
https://www.hackerrank.com/challenges/string-similarity/editorial
private void radixSort0() {
globalPtr = 0;
Arrays.fill(bucketHead, -1);
Arrays.fill(next, -1);
for (int i = 0; i < n; i++) {
int value = nr0[index[i]];
if (bucketHead[value] == -1) bucketHead[value] = bucketTail[value] = globalPtr;
else bucketTail[value] = next[bucketTail[value]] = globalPtr;
pointTo[globalPtr++] = index[i];
}
int ptr = 0;
for (int i = 0; i < M; i++)
for (int j = bucketHead[i]; j != -1; j = next[j])
index[ptr++] = pointTo[j];
}
This radixSort0() is not a complete radix sort. If your goal is to learn about radix sort, look elsewhere.
In both (needlessly duplicated) radixSort methods, int[] next is used to establish singly linked lists - using indexes instead of references, and -1 instead of null. (You can not just set next[some_index_depending_on value] to index[i] - there would be no lists.) The int[] pointTo would probably be more descriptively be named value. Think of next&value as linked lists, represented in an instance with two data members of type array, as an alternative to an array of instances with members next&value. globalPtr is the smallest index not yet allocated in that/those array/s.
(The blaring lack of comments in the code to follow is owing to my lack of understanding why anyone should try and construct a suffix array using this, or what the pieces of code contribute to that goal: feel free to correct&amend.)
Not even thinking about testing, the Java way of handling this might be
private void radixSortStep(int[]nr) {
List<Integer> value[] = new List[M];
for (int i = 0; i < value.length; i++)
value[i] = new ArrayList<Integer>(0);
for (int i: indexes)
value[nr[i]].add(i);
int ptr = 0;
for (int i = 0; i < M; i++)
for (int val: value[i])
indexes[ptr++] = val;
}
(with a bit of hand-waving about M (set to n+1) and nr1 (initialise entries not copied from rank to n, not -1))
import java.io.*;
import java.util.*;
public class St {
public static int calculate(String s){
char[]arr=s.toCharArray();
int length=arr.length;
int count=length;
for(int i=1;i<length;i++){
int len=length-i;
int j=0;
for(;j<len;j++)
if(arr[j]!=arr[j+i]){
break;
}
count+=j;
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner( System.in );
int n=scanner.nextInt();
for(int i=0;i<n;i++){
String s=scanner.next();
System.out.println(calculate(s));
}
}
}
it almost passed all testcases except last two due to timeout hope my work helps happy coding..
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).
I have a method called public int getSize() and it is suppose to return the number of items in the list. the instance variable is private Listable[] items; I thought it would just be something like this:
int size = 0;
for (int i = 0; i < items.length; i++){
size++;
}
return size;
But when I run it through these tests I get this nullpointerexception on the for (int i = 0; i < items.length; i++){ line
I don't think it likes items.length for some reason. I'm not getting any errors in Java. How should I do this?
i already tried return items.length;
that didnt work either.
http://www.easywayserver.com/blog/java-how-to-get-size-of-list/
I saw this article when I was browsing the web it contains the code that implements the list.size() method.
List<String> ls=new ArrayList<String>();
ls.add("one");
ls.add("Three");
ls.add("two");
ls.add("four");
int sizeOfList=ls.size();
System.out.println("Size of List :"+sizeOfList);
I believe you forgot to initialize the variable. Try something like:
items = new Listable[10];
For your getSize() method, you just need to return items.length
As MeBigFatGuy has commented (+1) your items variable is null. In fact, his comment entirely answers your question... Here's an implementation that should do what you want:
public int getSize() {
return items == null ? 0 : items.length;
}