This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I'm writing a hash table code. Taking mode according to table size.I want to start with table -1 and null.I understand that it is empty. Java hash table ready.So I do not find many examples.
My fill_in method is not running.
class Node{
int index;
int number;
Node next;
public Node(int index,int number,Node next){
this.index=index;
this.next=next;
this.number=number;
}
}
class Table{
int max_row;
public Table(int size){
this.max_row=size;
}
Node rows[]= new Node[max_row];
public void fill_in(){
for(int i=0; i<max_row;i++)
rows[i]=new Node(-1,-1,null);
}
You should initialize your array in constructor, because now it is initialized with unknown max_row.
class Table {
int max_row;
Node rows[];
public Table(int size) {
this.max_row = size;
rows = new Node[max_row];
}
You are trying to set the size of your Node array to the size of the value in max_size before it has been initialized. Change your class to this
class Table{
int max_row;
Node rows[];
public Table(int size){
max_row = size;
rows = new Node[max_row];
}
public void fill_in(){
for(int i=0; i<max_row;i++)
rows[i]=new Node(-1,-1,null);
}
}
Related
This question already has answers here:
Method for adding Objects into a fixed collection(Array) in Java
(3 answers)
Closed 10 months ago.
In my program I have two private variable
private Object[] array;
private int place;
I have initialized these variables in the constructor
public Arrays() {
array = new Object[10];
place = 0;
}
I am trying to properly implement the following method, this is what I have so far
public boolean add(Object new) {
for(int j = place+1; j <= 0; j++){
array[j] = new;
}
place++;
return true;
}
I am having trouble with placing the object 'new' into the next unoccupied cell
Just assign to the next free slot. new is a reserved keyword.
public boolean add(Object newItem) {
vals[place++]=newItem;
return true;
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
I tried to build the node object with a arraylist element for adding child node, and initialized successfully. However when i tried to add nodes into the arraylist the exception happened. How to solve the problem?
public class nodes{
int value=-2;
boolean root=false;
nodes parent;
ArrayList<nodes>Children;
public nodes(int value,ArrayList Children) {
this.value=value;
this.Children=Children;
}
public void setParent(nodes parent) {
this.parent=parent;
}
public void Add(nodes child) {
this.Children.add(child);
}
}
public class TreeHeight {
int n;
int parent[];
nodes Nodes[];
void read() throws IOException {
FastScanner in = new FastScanner();
n = in.nextInt();
ArrayList[]Children=new ArrayList[n];
parent = new int[n];//parent is an array that contains all elements
Nodes=new nodes [n];
for (int i = 0; i < n; i++) {
//parent[i]= ;
//index[i]=i;
Nodes[i]=new nodes(in.nextInt(),Children[i]);
}
//Nodes[0].Add(Nodes[1]);
for (int vertex = 0; vertex < n; vertex++) {
if(Nodes[vertex].value==-1) {Nodes[vertex].root=true;
}
for(int j=0;j<n;j++) {
if(Nodes[j].value==vertex) {
Nodes[j].setParent(Nodes[vertex]);
Nodes[vertex].Add(Nodes[j]);
}
}
}
}
I don't see add values for this array?
ArrayList[]Children=new ArrayList[n];
but have get value in this line?
Nodes[i]=new nodes(in.nextInt(),Children[i]);
This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 4 years ago.
I have tried many different things to try to print this stack but it keeps printing the hashcode ie. Problem1$Node#3d4eac69. I have searched a lot online and found nothing that has worked for this so if there are any suggestions help is greatly appreciated.
import java.util.*;
public class Problem1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Reading the first integer containing the number of test queries
int N = input.nextInt();
// Initializing the maximum value
int max = Integer.MIN_VALUE;
// Initializing the stack.
Stack<Node> stack = new Stack<Node>();
// Read the query and perform the actions specified.
for(int i = 0; i < N; i++){
int x = input.nextInt();
if(x == 1){
int value = input.nextInt();
Node n = new Node(value);
stack.push(n);
System.out.println(stack);
}
else if(x == 2){
stack.pop();
}
else if(x == 3){
System.out.println(stack.peek());
}
}
}
static class Node{
int data;
public Node(int data){
this.data = data;
}
}
}
You need to override the default toString method (inherited from Object see API here ) in your Node class, something like this:
static class Node {
int data;
public Node(int data){
this.data = data;
}
#Override
public String toString() {
return "Node: "+data;
}
}
The toString method is used when you try to print the object as a String. If you don't have one, it will use the Object's one which builds a String this way
getClass().getName() + '#' + Integer.toHexString(hashCode()))
and gives you something like Problem1$Node#3d4eac69
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I wrote a code for keeping record of indices even after sorting but it is showing me null pointer exception.I read other threads on same topic but still couldn't find.
import java.io.*;
import java.util.*;
public class Solution {
class Order{
public int index;
public int sum;
public void setIndex(int index){
this.index = index;
}
public void setSum(int sum){
this.sum = sum;
}
public int getSum(){
return this.sum;
}
public int getIndex(){
return this.index;
}
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
Order array[] = new Order[N];
int index = 0;
int sum = 0;
while(index<N){
int input1 = sc.nextInt();
int input2 = sc.nextInt();
//line 32
array[index].setIndex(index);
array[index].setSum(input1+input2);
index++;
}
ArrayList<Order> list = new ArrayList<Order>(Arrays.asList(array));
Collections.sort(list, new Comparator<Order>(){
#Override
public int compare(Order o1, Order o2){
return(Integer.compare(o1.getSum(), o2.getSum()));
}
});
System.out.println(list);
}
}
error is like this :
Exception in thread "main" java.lang.NullPointerException
at Solution.main(Solution.java:32)
I am passing i, still null pointer why?
You initialized array with Order array[] = new Order[N];, but the array is full of null objects. You need to initialize every element with new Order and then use setters
I want to crate a Heap structure that each node have 2 data , 1) string 2) int
so i think that each node must be a Class that's name is "heapNode" , but i have a trouble in swap method ,
please help me
import java.util.ArrayList;
public class MainHeap {
ArrayList<heapNode> heap;
MainHeap (){
new ArrayList<heapNode>();
}
public int getMin(){
return heap.get(0).data ;
}
private int parent(int pos) {
return pos / 2;
}
private void swap(int pos1, int pos2) {
heapNode temp =new heapNode();
temp = heap.get(pos1);
heap.get(pos1) = heap.get(pos2);
heap.get(pos2) = temp;
}
public void insert(int elem) {
int max = heap.size();
heap.get(max).data = elem ;
int current = heap.size() ;
while (heap.get(current).data < heap.get(parent(current)).data){
swap ( current , parent(current));
}
}
}
and this is my heapNode class
public class heapNode {
int data;
String fileName;
}
the swap method has error but i cant solve errors
Your swap code actually makes the objects point to different objects. It does not modify the positions in the arraylist itself. If using arraylist, you will have to remove an object from an index and set that object at a new index to swap or else you can use other data structure.
java.util.PriorityQueue<YourClass>