Array-based implementation of the list interface - java

I am looking at some example code on Listinterface and the implementation of it using array. I don't understand the instance variable private Object items[]; // an array of list items So is it an object called items, which is an array? why doesn't it just say private array items[] ?
Also about the constructor items = new Object[MAX_LIST];does it initalise an array named item? The code is attached below. Many thanks for your help!
public interface ListInterface {
public boolean isEmpty();
public int size();
public void add(int index, Object item)
throws ListIndexOutOfBoundsException,
ListException;
public Object get(int index)
throws ListIndexOutOfBoundsException;
public void remove(int index)
throws ListIndexOutOfBoundsException;
public void removeAll();
}
public class ListArrayBased implements ListInterface {
private static final int MAX_LIST = 90;
private Object items[]; // an array of list items
private int numItems; // number of items in list

Basically, private Object items[]; is exactly equivalent to private Object[] items; -- it's just different way of writing an array of Object called items.

Object is a Generic. It can be any object including String, Integer, etc.. So here you are creating an Array of the type Object, with the size mentioned.
Better go through Oracle Java Tutorials about Arrays.

Related

How to implement a function that returns an "enumeration" of objects?

I am working on a project about design patterns using Java. The class diagram before using the design patterns is provided in the link. Since I am new to Java, some of the classes methods descriptions is ambiguous for me.
For example in a class called Mall which has a method stores(), the method stores() is abstractly described as returning an "enumeration" of the stores in the mall (I'm using Java arrays as a placeholder in my example code).
I'd like to know what could be an "enumeration" in Java, i.e. what should I use for the concrete return type? In that particular case we are expected to use the Iterator design pattern. Please provide an example if possible.
https://www.dropbox.com/s/kbug0ow3e14284b/DP_Project_1182.pdf?dl=0
public class Mall {
private String mallId;
private Store[] Stores;
private Customer[] customers;
public void enter(Customer c){}
public void exit(Customer C){}
public ShoppingCart getShopingCart()
{
return new ShoppingCart();
}
public Customer[] customers()
{
}
public Store[] stores()
{
}
void addStore(Store S)
{
}
}
You need to create an abstract class called Store as per your objective.
To return Enumeration of Iterator type, I have created a List and returned its elements as an Iterator.
I hope this example helps:
public abstract class Store {
private String name;
private Item items;
//..other items
}
public class Mall {
private Store myStore;
//Vector which Stores all Store objects
private List<Store> listOfStores=new LinkedList();
//stores method which returns an Enumeration of Stores*/
public Iterator<Store> stores(){
return listOfStores.iterator();
}
public void addStore(Store s){
listOfStores.add(s);
}
}

Assigning ArrayList object to Instance variable - java

The code snippet below is part of some code I am reading for an assignment but I cant understand the role of the copy variable in the snippet or what it does. I know its an instance of the Sample class, but why it is then assigned an ArrayList is not clear to me.
public class Sample implements Var{
private List lst1;
private List lst2;
public Sample() {
super();
}
public Sample(List lst1) {
this();
this.lst1 = lst1;
}
public List getLst1() {
return lst1;
}
public void setLst1(List lst1) {
this.lst1 = lst1;
}
#Override
public Var copy(){
Sample copy = new Sample(lst1);
copy.lst2 = new ArrayList(lst2);
return copy;
}
#Override
public void randomize(){
}
}
In fact the error message is explicit to show that you can't iterate over the variable copy because you haven't implemented the Iterable interface which allows you to do it. If you insist to loop over it and to have functions allowing you to do so: just visit this link Java Generics - Implementing the Iterable Interface where you can for exemple (if this is what you want) iterate over the elements of the two lists of an instance lst1 and lst2

Accessing Objects from a 2d Arraylist of Custom Objects in Java

Consider the following case. I have 2 classes as shown below:
public class CustomObject{
public int a;
public String xyz;
public ArrayList<Integer> arrInt;
public SomeOtherClass objectSOC;
public CustomObject(){
//Constructor
}
/*Followed by other methods in the class*/
}
Now there is another class in which I have created ArrayList[][] of CustomObject, the way shown below
public class CustomObjectUtil{
ArrayList<CustomObject>[][] arrCO = new ArrayList[100][100];
public CustomObjectUtil(){
//Assume there is an object of CustomObject class, let's call it ObjectCO, and a method that adds values to the arrCO using arrCO[i][j].add(ObjectCO);
//Now, here I want to access objects from my 2D ArrayList as
String stringCO = arrCO[indx][indy].xyz;
ArrayList<Integer> arrIntCO = arrCO[indx][indy].arrInt;
SomeOtherClass objectSOC_CO = arrCO[indx][indy].objectSOC;
// But the above method is not allowed;
}
}
I could not find a way to do this type of assignment. Please comment if you need more info!
Object referenced by arrCO[indx][indy] is an ArrayList
arrCO is a 2-D array of List of CustomObject
do this to access what you're trying to access:
List<CustomObject> customObjList = arrCO[indx][indy];
CustomObject customObj = customObjList.get(0) // assuming there are elements in this list
Now you can access arrInt & objectSOC as
customObj.arrInt & customObj.objectSOC

Customer ArrayList Class amending Specific Element in Java

How do i change a specific variable at a specific point in a custom ArrayList? I have created an ArrayList class and need to adjust a certain variable within the list, but i cannot seem to figure how i would achieve it. I have searched for the answer as well but nothing seems specific to my problem.
The code for my Super Class is:
public class Parcel extends JPanel
{
protected int idNum;
protected double charge;
protected char zone;
protected ImageIcon i;
protected static ArrayList<Parcel> parcelList;
protected static ParcelList newParcel = new ParcelList();
public Parcel(int id, char z)
{
this.idNum = id;
this.zone = z;
}
And the code for my list is...
public class ParcelList extends Parcel
{
ParcelList()
{
parcelList = new ArrayList<Parcel>();
}
public void addBox(int id, char z, int w, int l, int h)
{
parcelList.add(new Box(id,z,w,l,h));
}
What i am looking to do is change the ImageIcon for example, change the ImageIcon of the 5th element in the List of list that contains many different instances of Box with differing images. And also Removing an element.
Is it possible to do this with the current way i have set my code up?
Never minding what seems to be some redundancy in the code, one way to do this is to use newParcel.get(index), modify the Box that you get from it using setter methods, then newParcel.set(index, newBox)

How to create ListView from an Object with multiple arrays

I am lost again. My object is as follows:
public class LogInfo implements Serializable{
public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
public int[] times;
...
}
First of all I populate the ListView from an ArrayList of this objects. Then I select an object from the ListView, and I would like to populate new list in new fragment with fields
public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
However, to create an ArrayAdapter I can't just pass an object to it (as I understand). I need to pass an array. the problem is, that I would like to pass an object previously created and selected, and then populate the list from its fields (not only strokes or codes, but both of them).
How should my ObjectAdapter class look like and what class should it extend? To select an Object from ArrayList of Objects I used:
public class LogInfoObjectAdapter extends ArrayAdapter<LogInfo>
Example (real life):
I have a lot of cars on the parking and I need to make a list of them, so I use ArrayAdapter to populate the list. After I choose one car from the list (car object) it has two arrays in it (for example broken bulbs and broken tires, but both array are same size). Now I want to will the new list with information from selected car. I hope it is clear enough. My problem is that to use ArrayAdapter I have to pass an array in the constructor, but I want to pass the whole object and inside my adapter class process it and add choosen fields to ListView
If you have an object with multiple lists, you don't need to extend ArrayAdapter, you can just extend the BaseAdapter and implement the methods needed (getCount(), getView(), etc).
public class Adapter extends BaseAdapter {
class LogInfo implements Serializable {
public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
public int[] times;
}
private LogInfo mInfo;
public Adapter(LogInfo info) {
mInfo = info;
}
#Override
public int getCount() {
if (mInfo != null && mInfo.strokes != null) {
return mInfo.strokes.size();
}
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (mInfo != null) {
Point[][] p = mInfo.strokes.get(i);
byte[] b = mInfo.codes.get(i);
//create the view
}
return null;
}
}
1) Array adapter has method getItem() you can use it to get particaular item by index.
2) Make your LogInfo implements IIterable interface
http://developer.android.com/reference/java/lang/Iterable.html
public class LogInfo implements Serializable, Iterable<Point[][]>{
public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
public int[] times;
public abstract Iterator<Point[][]> iterator (){
//Iterator implementation
}
Now you can use this object directly in other list whitch has following signature
public class LogInfoObjectAdapter extends ArrayAdapter<Point[][]>

Categories

Resources