I'm trying to create some methods for my object but the outside of the constructor, the object isn't recognized by it's name
public class Playlist extends SongRecord {
final int maxSongs = 50;
public Playlist() {
SongRecord[] list = new SongRecord[maxSongs];
}
public int size(){
return list.length();
}
}
The error message says list isn't recognized
Because your list scope is in the constructor if u want to access list every where u need to move your list to class scope
public class Playlist extends SongRecord {
final int maxSongs = 50;
SongRecord[] list;
public Playlist() {
list = new SongRecord[maxSongs];
}
public int size(){
return list.length();
}
}
Just move list to class scope:
public class Playlist extends SongRecord {
final int maxSongs = 50;
private SongRecord[] list;
public Playlist() {
list = new SongRecord[maxSongs];
}
public int size(){
return list.length();
}
}
Related
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.
I'm trying to have my method "add" access the contents of an ArrayList that was created in method "Friends", but Java isn't happy with what I'm doing (scope issues?). Is there a way to fix the problem without having to pass arguments?
public class Friends {
public Friends(float x, float y)
{
ArrayList<MyObject> arrayList = new ArrayList<MyObject>();
MyObject[] friendList = new MyObject[20];
}
public void add()
{
for (int i = 0; i < 20; i++) {
//friendList[i]
}
}
}
note that Friends is meant to be a constructor (if I'm using that word right)
Obviously for such cases you should use such called "object variables", or simply saying - fields of the class. You should make your variable arrayList part of the class as a field:
public class Friends {
List<MyObject> arrayList;
public Friends(float x, float y)
{
arrayList = new ArrayList<MyObject>();
MyObject[] friendList = new MyObject[20];
}
public void add()
{
for (int i = 0; i < 20; i++) {
//arrayList.add(...).
}
}
}
Make your variables member variables of the Friends class:
public class Friends {
ArrayList<MyObject> arrayList;
MyObject[] friendList;
public Friends(float x, float y)
{
arrayList = new ArrayList<MyObject>();
friendList = new MyObject[20];
}
public void add()
{
for (int i = 0; i < 20; i++) {
//friendList[i]
}
}
}
Your guess is correct. The problem here is scoping. You are creating a local variable arrayList in the constructor, which is only available in the constructor.
You should declare it as an instance variable like this:
public class Friends {
ArrayList<MyObject> arrayList; = new ArrayList<MyObject>();
MyObject[] friendList; = new MyObject[20];
public Friends(float x, float y)
{
this.arrayList = new ArrayList<MyObject>();
this.friendList = new MyObject[20];
}
public void add()
{
for (int i = 0; i < 20; i++) {
//friendList[i]
}
}
}
This is how my class definition goes:
public class ArrayBag implements Bag {
/**
* The array used to store the items in the bag.
*/
private Object[] items;
/**
* The number of items in the bag.
*/
private int numItems;
... and so on...
This is a method in the class definition, where a new object of this class is created inside the method:
//creates and returns an Arraybag that is the union of the called Arraybag and the parameter bag
public bag unionWith(Bag other) {
if (other == null)
throw new IllegalArgumentException();
int cap = this.capacity() + other.capacity();
//new object created here
ArrayBag newbag = new ArrayBag(cap);
for (int i = 0; i < numItems; i++) {
if (other.contains(items[i]))
newbag.add(items[i]);
}
for (int i = 0; i < newbag.numItems(); i++)
//Can I use "newbag.items[i]"?
if (numOccur(newbag.items[i]))
}
My question is, can I access the Object[] items of the newbag object from inside this method definition? Like this: newbag.items[i]
You can access it.
It is feasable to do:
public class AClass {
private int privateInteger;
public AClass() {
privateInteger = 5;
}
// First way
public void accessFromLocalInstance() {
AClass localInstanceOfClass = new AClass()
int valueOfLocalInstance = localInstanceOfClass.privateInteger;
}
// Second way
public void accessFromInstance(AClass instance) {
int valueOfInstance = instance.privateInteger;
}
}
because
"private" means restricted to this class, not restricted to this object.
See Access private field of another object in same class
I am writing an iterator method for my own array list class, however, when I try to test the class it says foreach loop is not applicable to MyArrayList. can anyone help me out with where I have gone wrong?
The class itself uses arrays of objects and the necessary methods to act like an arraylist (add, remove, get etc.)
Here is the class constructor and my iterator class:
public class MyArrayList {
public Object[] arrayList = new Object[5];
int length1 = 5;
public MyArrayList() {
}
public MyArrayList(Object[] arrayList) {
this.arrayList = arrayList;
length1 = arrayList.length;
}
public ArrayListIterator iterator(){
return new ArrayListIterator(this);
}
class ArrayListIterator<MyArrayList> implements Iterator<Object> {
private Object[] arrayListIterable;
private int count = 0;
public ArrayListIterator(Object[] x){
arrayListIterable = x;
}
public ArrayListIterator(MyArrayList myArrayList) {
}
public boolean hasNext(){
if(count < arrayList.length){
return true;
}else{
return false;
}
}
public Object next(){
int x = count;
count++;
return arrayListIterable[x];
}
}
Your MyArrayList class must implement Iterable<T> interface.
Check the javadoc for Iterable:
Implementing this interface allows an object to be the target of the "for-each loop" statement.
public class TableModel extends AbstractTableModel {
public int page;
public TableModel(Integer p) {
this.page=p;
System.out.println("mm"+page);
}
public void pudata() {
System.out.println(page);
}
//System.out.println("model "+page);
private String[] columnNames = {"groupName","membersCount","previliage"};
public ArrayList<GroupData> data = (new DatabaseLayer ()).getGroup(page);
#Override
public int getRowCount() {
return data.size() ;
}
Can not access variable page in getgroup() method it passes 0 to getgroup() method.
public ArrayList<GroupData> data = (new DatabaseLayer ()).getGroup(page);
Your question is unclear, but I suspect the problem is just that all the instance initializers are being run before the constructor body, so you're seeing the default value for page. You should have something like:
public class TableModel extends AbstractTableModel {
private static final String[] columnNames =
{"groupName","membersCount","previliage"}; // TODO: Fix spelling!
private final int page;
private final List<GroupData> data;
public TableModel(int page) {
this.page = page;
this.data = new DatabaseLayer().getGroup(page);
}
...
}
It's generally a good idea to keep all your instance/static variable declarations in one place (I prefer to keep them at the top, but YMMV) and make them all private to make it easier to reason about how they're used. The main change, however, is moving the new DatabaseLayer ().getGroup(page) code into the constructor.
public class TableModel extends AbstractTableModel {
public int page;
public ArrayList<GroupData> data;
public TableModel(Integer p) {
this.page=p;
this.data = (new DatabaseLayer ()).getGroup(page);
System.out.println("mm"+page);
}
public void pudata() {
System.out.println(page);
}
//System.out.println("model "+page);
private String[] columnNames = {"groupName","membersCount","previliage"};
#Override
public int getRowCount() {
return data.size() ;
}
Refresh your data field every time when you assign a new value to the page field.
public TableModel(int p) {
setPage(p);
}
public void setPage(int p) {
this.page = p;
this.data = new DatabaseLayer ().getGroup(page);
}
This is absolute correct because:
public int page;
default value for page is 0 because its int.
public ArrayList<GroupData> data = (new DatabaseLayer ()).getGroup(page);
Is a variable initialization so before initialization of page you are passing it into .getGroup(page) so default value will pass in that case.
So you have to call getGroup(int) method after page being initialized, one way can be following:
private final List<GroupData> data;
public TableModel(Integer p) {
this.page = p;
this.data = new DatabaseLayer().getGroup(page);
System.out.println("mm"+page);
}