Reputation: 305
I don't normally use Java and I'm currently trying to help a friend out with a java assignment and this has me stuck
I'm trying to access an array that I create in the constructor of an Object but I cannot figure out how to access it.
public class ADTbag {
String item = "Testing";
public ADTbag(int size) {
// This constructor has one parameter, name.
String[] bag = new String[size];
bag[0] = Integer.toString(size);
System.out.println("A bag was created with the size of " + size + " | " + bag[0]);
}
public void insert() {
/* Insert an item */
/* One Problem this public void doesn't have access to the bag var"
System.out.println(bag);
}
I feel like this is a simple concept in java but I cannot find anything on my googles that has helped me. I want to be able to insert something in the bag or string array object using the insert method. So something like this.
public static void main(String []args) {
/* Object creation */
ADTbag myBag = new ADTbag(5);
String value = "Some Value";
/* I want to do this */
mybag.insert(value);
}
}
Upvotes: 1
Views: 103
Reputation: 352
First of all you have to make the bag field something global. After that we can create a function to insert/add a new element to your bags. Then is not necessary work with constructor, as you're trying to do.
Another thing is, as you're talking about inserting and/or adding itens to a "list", is adequated use ArrayList
instead a standard array
.
ArrayList is a data/collection structure that ables you to add, remove, set, get (and some other operations) at runtime above the same object. If we want to insert a new item inside a array we can't; for this we have to create another array with size+1 and after set all elements of the new array. Then this is so confusing for a simple operation.
Thinking in this I'll give you a approach that uses this, take a look:
import java.util.ArrayList;
public class ADTbag {
/*
global field to be referenced through entire class.
We have to specify the type of objects that will be inserted
inside this list, in this case String
*/
ArrayList<String> bag;
String item = "Testing";
//constructor doesn't need parameter
public ADTbag() {
//here we init the bag list
bag = new ArrayList();
//adds your "standard item" on creating
bag.add(item);
/*
prints your msg.
- to get the size of a ArrayList just call list.size();
- to get the item from the X index just call list.get(X)
*/
System.out.println("A bag was created with the size of " + bag.size() + " | " + bag.get(0));
}
/*
you doesn't need a new method
*/
}
To use do this:
public static void main(String[] args) {
ADTbag myBag = new ADTbag();
myBag.bag.add("some value");
}
Upvotes: 0
Reputation: 1361
Define variable as instance variables
public class ADTbag {
String item = "Testing";
String[] bag;
public ADTbag(int size) {
// This constructor has one parameter, name.
this.bag = new String[size];
bag[0] = Integer.toString(size);
System.okaut.println("A bag was created with the size of " + size + " | " + bag[0]);
}
public void insert() {
/* Insert an item */
/* One Problem this public void doesn't have access to the bag var"
System.out.println(bag);*/
}
}
Which will look like above.
Upvotes: 0
Reputation: 2599
You need to make bag
a class member so it is accessible outside of the constructor.
Upvotes: 1