duplicate results with array of array in class - java

I am new with java and I need to save the content of a stack of objects into an array which will have an array attribute and also the array attribute has an array attribute I know it kind complicated but I need to do that because it's looks like a table. Anyway, this is my code
//First class
public static class classA //inner
{ public char sy;
public ArrayList <Integer> arrystateA;
public classA()
{ sy='0';
arrystateA= new ArrayList <Integer>();} }
//The second class
public static class ArrayofclassA// meddile
{ int num;
public ArrayList <classA> arrystateB;
public ArrayofclassA()
{ arrystateB=new ArrayList < classA>();
num=-1;}
public void creatrans(classA z)
{ arrystateB.add(z);
} }
and this the main code
classA temp2=new classA();
ArrayofclassA temp3 = new ArrayofclassA();
ArrayList<ArrayofclassA> NFAtranstable=new ArrayList<ArrayofclassA>();
while(!reslt.empty()){
temp=reslt.pop();
for (int i=0;i<NFAtranstable.size();i++)
{temp3=NFAtranstable.get(i);
if (temp3.num==temp.s_to)
{
if(temp3.arrystateB.get(i).sy==temp.t_sym)
{
temp3.arrystateB.get(i).arrystateA.add(temp.s_to);
match=true;
}else
{temp2.sy=temp.t_sym;temp2.arrystateA.add(temp.s_to);
temp3.creatrans(temp2);
match=true;
}
}
}
if(!match)
{
temp2.sy=temp.t_sym;
temp2.arrystateA.add(temp.s_to);
temp3.num=temp.s_from;
temp3.arrystateB.add(temp2);
NFAtranstable.add(temp3);
}
match=false;
}
for(int i=0;i<NFAtranstable.size();i++)
{
System.out.print(NFAtranstable.get(i).num+" ");
for(int j=0;j<NFAtranstable.get(i).arrystateB.size();j++)
{
System.out.println(NFAtranstable.get(i).arrystateB.get(j).sy+" ");
for(int k=0;k<NFAtranstable.get(i).arrystateB.get(j).arrystateA.size();k++)
{
System.out.print(NFAtranstable.get(i).arrystateB.get(j).arrystateA.get(k));
}
}
}
}
the problem that when I print the content of the array 'NFAtranstable' it's gives me duplicate results like it just save the same valus, it's been about 8 hours and I couldn't find the problem =(

Related

How to pass my object into another objects field?

I am trying to fill UCFCourse courseOne in my constructor with a courses[] object in fillWithCourses().UCFCourse courseOne does populate outside of the constructor but will not go into it.
public class UCFSemester<courses> {
private static UCFCourse courseOne;
private static double totalSemesters;
private static double completionTime;
static boolean fillSemester = true;
public UCFSemester(UCFCourse courseOne, UCFCourse[] coursetwo) {
this.courseOne = courseOne;
}
public static UCFCourse getcourseOne() {
return courseOne;
}
public static void setCoursesone(UCFCourse courses) {
courseOne = courses;
}
public static void fillWithCourses(UCFCourse courses[], int l) {
int x = 0;
while (fillSemester) {
for (int n = 0; n < 5; n++) {
if (x != n && courses[x].getCourseLevel() < courses[n].getCourseLevel()) {
setCoursesone(courses[x]);
}
}
fillSemester = false;
}
}
}
Side question.How can I access this all in a non-static way?I need the entire thing to be non-static but no matter what I do I can't get it.Thanks!
You can simply do it by creating a List like this:
public class UCFSemester {
private List<UCFCourse> courseList = new ArrayList<>();
public UCFCourse getCourse(int index) {
return courseList.get(index);
}
public void addCourses(UCFCourse[] courses) {
for(int x = 0; x < courses.length; x++) {
courseList.add(courses[x]);
}
}
}
Here, I'm assuming that you are passing the UCFCourse[] array with all the course details that are there in that particular semester.
addCourses() function will take this array and then add all the corresponding courses to the List.
getCourse() function will return you any particular course from the List (Using Index). You can also modify the search in any way you want.

Java heap space exception when trying to fill a list

I'm writing a program in java and i need to make a list whose nodes are another list . My code for the nodes of the sub list is this :
public class Page {
private String word;
private int num;
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Page(String word, int num) {
this.word = word;
this.num = num;
}
}
My code for the nodes of my main list is :
import java.util.ArrayList;
public class IndPage {
private ArrayList<Page> Eggrafi;
//private ArrayList<Page> Eggrafi = new ArrayList<Page>();
public IndPage(String name, int bytes) {
Eggrafi = new ArrayList<Page>();
Eggrafi.add(new Page(name, bytes));
}
public ArrayList<Page> getEggrafi() {
return Eggrafi;
}
public void setEggrafi(ArrayList<Page> eggrafi) {
Eggrafi = eggrafi;
}
}
When i use in my main the following code to fill my list i get a java heap space exception:
if(Index.size()!=0){
for(int j=0;j<Index.size();j++){
for(int y=0;y<Index.get(j).getEggrafi().size();y++){
if((Index.get(j).getEggrafi().get(y).getWord()).equals(tokens[i-1])){
Index.get(j).getEggrafi().add(new Page(fileName[k],byte_count));
}
else{
Index.add(new IndPage(fileName[k],byte_count));
}
}
}
}
else{
Index.add(new IndPage(fileName[k],byte_count));
}
Also my main list is declared this way :
List<IndPage> Index = new ArrayList<IndPage>();
I've tried many things but still getting the java heap space exception .
Your issue is in your for loop:
for(int j=0;j<Index.size();j++){
for(int y=0;y<Index.get(j).getEggrafi().size();y++){
if((Index.get(j).getEggrafi().get(y).getWord()).equals(tokens[i-1])){
Index.get(j).getEggrafi().add(new Page(fileName[k],byte_count));
}
else{
Index.add(new IndPage(fileName[k],byte_count));
}
}
}
Your for loops are doing a check against the lists .size() function you are adding new items to those lists, so the .size() will always be at least 1 more than either the j or y index variables and the loops will never terminate. That eventually is running you out of heap space. The Index.size() and Index.get(j).getEggrafi().size() values are recalculated each time by the for loop, they are not cached.
I think you code is allocating a new list for every insert.
import java.util.List;
import java.util.ArrayList;
public class IndPage {
private List<Page> Eggrafi = new ArrayList<Page>();
public IndPage(final String name, final int bytes) {
Eggrafi.add(new Page(name, bytes));
}
public List<Page> getEggrafi() {
return Eggrafi;
}
public void setEggrafi(final List<Page> eggrafi) {
Eggrafi = eggrafi;
}
}
The code for the loops can be improved by using Java 5 style collection loops ie:
for (final Page page : Eggrafi) {
...
}

add a class to a 2d array? Java

I need to create a map which has randomly placed trees and food. For the map i am looking to create a 2d array and then add the trees and food to the array with use of classes, is this possible?
class Tree {
char tsymbol;
}
class Food {
char fsymbol;
}
The classes only have to have a symbol stored in them for now, which will be the icon which represents the objects on the map.
public void printWorld (){
int a;
int b;
char[][] map = new char[15][12];
for (a=1; a<=15; ++a)
{
for (b=1; b<=12; ++b)
{
foodsymbol.fsymbol = 1;
final String[] items = { };
map[a][b] =
System.out.print(map[1][1]);
}
System.out.printf("\n\n");
}
}
So far i have this, ignore the comments as that was my previous work. This part is where i am trying to add the objects to the array randomly, however i am having problems accessing the variables or classes in this method.
Thanks in advance for any help:)
Yes, you can have an array of objects, and print them. To have different types of objects in the same array, they should all implement a common interface. For instance:
interface MapObject {
public char getSymbol();
}
class Tree implements MapObject {
char tsymbol = 't';
#override
public char getSymbol() {
return tsymbol;
}
}
class Food implements MapObject {
char fsymbol = 'f';
#override
public char getSymbol() {
return fsymbol;
}
}
class MyClass {
public void printWorld() {
MapObject[][] map = new MapObject[15][12];
map[0][0] = new Tree();
map[0][1] = new Food();
System.out.print(map[0][0].getSymbol());
System.out.print(map[0][1].getSymbol());
}
}
For your particular application, you might also want to consider using an enum. This is useful if all trees are identical and all food is identical. It might look something like this:
enum Item {
TREE ('t'),
FOOD ('f');
private final char symbol;
Item(char symbol) {
this.symbol = symbol;
}
char getSymbol() { return symbol; }
}
class MyClass {
public void printWorld() {
Item[][] map = new Item[15][12];
map[0][0] = Item.TREE;
map[0][1] = Item.FOOD;
System.out.print(map[0][0].getSymbol());
System.out.print(map[0][1].getSymbol());
}
}

Create a Dictionary using Array of Object

I have a problem with this code. My purpose is to create a Dictionary that counts the frequency of a word in a text, using an Array of Objects (I can't use Hash Map or something else). I created a class Pair that contains the couple (word,count).
public class Pair
{ public String word;
public int count;
public Pair(String word,int count)
{this.word=word;
this.count=count;
}
public String getWord()
{return word;}
public int getCount()
{return count;}
public void addCount()
{count++;}
public String toString()
{ return getWord()+" "+getCount();}
}
And the class Dict that creates an Array of object using the Pair class
public class Dict
{ private Pair [] a;
private int inputSize;
public Dict()
{a=new Pair[10];
inputSize=0;
}
public void insert(Pair x)
{ if(a.length==inputSize)
{ Pair newA []=new Pair [2*inputSize];
for(int i=0;i<inputSize;i++)
{ newA[i]=a[i];
}
a=newA;
}
for(int i=0;i<inputSize;i++) // i check if x is already in the array if i find it i replace it otherwise i add it in the array
{ if(a[i].getWord().equals(x.getWord()))
{a[i]=x;
}
}
a[inputSize++]=x;
}
public Pair find(Pair x) // if i don't find x return null
{ for(int i=0;i<inputSize;i++)
{ if(a[i].getWord().equals(x.getWord()))
{return a[i];}
}
return null;
}
public String toString()
{String s="";
for(int i=0;i<inputSize;i++)
{ s=s+a[i].toString()+'\n';
}
return s;
}
}
After I created the test class with the main method
import java.util.*;
import java.io.*;
public class MyDict
{public static void main(String [] args)
{ Dict d=new Dict();
Scanner c=new Scanner(System.in);
while(c.hasNext())
{String s=c.next();
Pair p=new Pair(s,1); // create a new pair
Pair previous=d.find(p);
if(previous!=null) //if the pair is already in the stack i add 1 to the counter otherwise i insert it in the array
{p.count++;}
else
{d.insert(p);}
s="";
}
System.out.println(d);
}
}
But it doesn't work, in particular the variable "count" doesn't grow.
For example, if I write "how how are are you you " I get:
how 1
are 1
you 1
Can anyone help me please?
Change p.count++ to previous.count++.
Otherwise you never change the count of the existing Pairs.
Pair p=new Pair(s,1);
Pair previous=d.find(p);
if(previous!=null) {
previous.count++;
} else {
d.insert(p);
}

String-Output: Class##

Lets say I have three classes:
Class A:
public class A {
private String s;
public A() {
s = "blah";
}
public void print() {
System.out.println(s);
}
}
Class B:
public class B{
private A a[];
public B(){
a = new A[100];
for (int i=0; i<100;i++) {
a[i] = new A();
}
}
public void print() {
for (int i=0; i<100; i++) {
a.print(); //SHOULD BE a[i].print();
}
}
}
Class Main:
public class Main {
public static void main(String args[]) {
B b = new B();
b.print();
}
}
Why do I get an outputpattern like B##, where # is a number. I think it has something to do with indirect adressing but im not quite sure. Why doesn't it print out 100 s?
You are printing the array rather than the object in the array. As a result, it is printing the address of the object (the number) and the object it is a member of.
I suspect you wanted to call each of the prints, you should, in B.print(). You are also missing an increment for i, meaning it will loop indefinitely.
for(int i = 0; i < 100; ++i)
{
a[i].print();
}

Categories