I have a homework about implementing lists in java. I have written a code, and a method about displaying the elements, but when I run it, it says there is an error in this method. can you please help me fix this?
here is my code:
public class Lista {
public int num;
public Lista pas;
public Lista(int num){
this.num = num;
}
public void display(){
System.out.println(num);
}
public static void main(String[] args){
linkedList l = new linkedList();
l.insertfirst(1);
l.insertfirst(3);
l.insertfirst(5);
l.display();
}
}
class linkedList{
public Lista LIST;
public Lista pozicion;
linkedList(){
LIST = null;
}
public void insert(int num, Lista pozicion){
Lista temp = pozicion.pas;
Lista l = new Lista(num);
pozicion.pas.num = num;
pozicion.pas.pas = temp;
}
public void delete(Lista pozicion){
pozicion.pas = pozicion.pas.pas;
}
public Lista locate(int num, Lista LIST){
pozicion = LIST;
while (pozicion.pas != null){
if (pozicion.pas.num == num){
return pozicion;
}else{
pozicion = pozicion.pas;
}
}
return pozicion;
}
public void insertfirst(int num){
Lista eRe = new Lista(num);
eRe.pas = LIST;
LIST = eRe;
}
}
Well, for starters...
display() is not a method of class linkedList (note: naming convention should be LinkedList).
display() is a method of Lista. That is why the IDE is telling you 'display() is undefined for the type linkedList'
Just quickly looking at your code for what you need to do... You need to implement a get() (or perhaps a solution to get the first element of the linkedList and from there iterate through the linkedList) method in the linkedList class which returns a Lista object. With that Lista object, you can then call the method .display() on it.
Related
I have a problem regarding the display of my LinkedList with a foreach.
When I want to display my LikedList an error comes from my list which tells me that I have to use an iterator. Here is the code:
public class Set {
private LinkedList <Integer> elements;
public Set () {
this.elements = new LinkedList <Integer> ();
}
public static void main (String [] args) {
Set test = new Set ();
test.add (8);
test.add (2);
test.add (7);
for (int e: test) {
}
}
So I used an iterator for the display but a message tells me that my iterator is undefined for the method set.
Here is the code:
public class Set {
private LinkedList <Integer> elements;
public Set () {
this.elements = new LinkedList <Integer> ();
}
public static void main (String [] args) {
Set test = new Set ();
test.add (8);
test.add (2);
test.add (7);
Iterator <Integer> iterator = test.iterator ();
while (iterator.hasNext ()) {
System.out.println (iterator.next ());
}
}
If you can help me with that, it will help me a lot.
(I tried to define "Iterator <Integer> iterator = this.elements.iterator ();" in the Set () public and call it in the hand that way but without success "while (iterator.hasNext ()) {
System.out.println (iterator.next ());
}")
Thank you in advance, nice day
Your Set class needs to implement iterable interface as
public class Set implements Iterable<Integer> {
private LinkedList<Integer> elements;
public Set() {
this.elements = new LinkedList<Integer>();
}
#Override
public Iterator<Integer> iterator() {
return elements.iterator();
}
}
Your class Set doesn't define an iterator you should add for example a method like that one:
public Iterator<Integer> getIterator() {
return elements.iterator();
}
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.
I have created my own linked list implementation that stores a first name and a last name. Now I am trying to sort the list of names alphabetically by the last name, which I am having trouble doing. I have read about possibly using collections.sort but I am not sure how I can modify my class so I can use collections.sort or if that is even a good way to go about it or not. Below is what I have done so far. If anyone could help me with this it would be greatly appreciated. What I want to accomplish is that when I print it will print Joe Rogers, Bill Thomas, Greg West, which would mean it was sorted by last name.
Edit: Thanks everyone for your quick responses, I really appreciate it.
class Test {
public static void main(String[] args)
{
LinkedList l = new LinkedList();
l.insert("Greg", "West");
l.insert("Joe", "Rogers");
l.insert("Bill", "Thomas");
l.print();
Collections.sort(l) // I am unable to get this to work
}
public static class myLink {
public myLink next;
public String first;
public String last;
public myLink(String first, String last)
{
this.first = first;
this.last = last;
}
public void print()
{
System.out.printf("%s %s\n",first,last);
}
}
public static class LinkedList {
private myLink linkedList;
public LinkedList()
{
linkedList = null;
}
public void insert(String first, String last)
{
myLink li = new myLink(first, last);
li.next = linkedList;
linkedList = li;
}
public void print()
{
myLink c = linkedList;
while(c != null)
{
c.print();
c = c.next;
}
}
}
}
LinkedList will work for you. You only need your Person class, and a customised Comparator as in below example.
public class Person {
String first;
String last;
public Person(String f, String l) {
this.first = f;
this.last = l;
}
}
public class LinkExample {
public static void main(String[] args) {
LinkedList<Person> pList = new LinkedList<Person>();
pList.add(new Person("AFirst","ZLast"));
pList.add(new Person("BFirst","BLast"));
Collections.sort(pList, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.last.compareTo(p2.last);
}
});
System.out.println(pList.pollFirst().last);
System.out.println(pList.pollFirst().last);
}
}
I have tried using Java's linked list, but the add method only lets you use a single string
No, you can add your class as well. Simply implements Comparable interface add put your logic in overridden compareTo() method.
Use Collections.sort() to sort the list.
sample code:
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.add(new MyLink("Greg", "West"));
l.add(new MyLink("Joe", "Rogers"));
l.add(new MyLink("Bill", "Thomas"));
Collections.sort(l);
for(MyLink link:l){
link.print();
}
}
public static class MyLink implements Comparable<MyLink>{
public MyLink next;
public String first;
public String last;
public MyLink(String first, String last) {
this.first = first;
this.last = last;
}
public void print() {
System.out.printf("%s %s\n", first, last);
}
#Override
public int compareTo(MyLink o) {
int result = this.last.compareTo(o.last);
if(result==0){
result = this.first.compareTo(o.first);
}
return result;
}
}
output:
Joe Rogers
Bill Thomas
Greg West
You can achieve it without implementing Comparable interface.
Simply use Collections.sort(List,Comparator) method.
sample code:
Collections.sort(l,new Comparator<MyLink>(){
#Override
public int compare(MyLink o1, MyLink o2) {
int result = o1.last.compareTo(o2.last);
if(result==0){
result=o1.first.compareTo(o2.first);
}
return result;
}
});
Note:
Never use build in class's name for your class that might sometime create confusion
Respect Java Naming convention.
I'd create a Person bean object which:
contains the firstname and surname, and
implements Comparable to sort on the surname
Then all you have to do is put all your Person objects in an ArrayList, and use Collections.sort() to get them in the right order.
You can have your class myLink implement Comparable interface. Then override compareTo() method as per your requirement.Then you can sort using Collections.sort(collection);
For more info read the documentation.
Also take care of Java naming conventions. myLink should be MyLink and so on...
This will sort the Collection of myLink . But if you want to sort your custom LinkedList class I would suggest create a method sort() in class LinkedList. In this method create a List of all your myLink instances (You have reference to the head). Then call Collections.sort() here and return Sorted Collection.
Suppose I have similar methods that does almost same thing How do I make it one method that will query different data. Here only the dataManager querying data is different and gets different Object Lists for iteration.
private void method1(ClassA classA){
List<One> one = dataManager.getOne(classA.param1);
if (one != null){
//iterate data here...
for (int i = 0; i < one.size(); i++)
{
one.get(i).getAttributeOne();
one.get(i).getAttributeTwo();
}
}else{
}
}
private void method2(ClassA classA){
List<Two> two = dataManager.getTwo(classA.param1);
if (two != null){
//iterate data here...
}else{
}
}
Do the call to dataManager.get() in the calling method, and use generics to handle the different list types.
private <T> void method1(ClassA classA, List<T> myList){
if (myList != null){
//iterate data here...
}else{
}
}
// Calling code
method1(myClassA, dataManager.getOne(myClassA.param1));
method1(myClassA, dataManager.getTwo(myClassA.param1));
Here is a compilable example that looks like what you are trying to solve.
import java.util.*;
public class Iterate{
private <T> void method1(int classA, List<T> L){
for (T t: L) {
System.out.println(t);
}
}
public static void main(String[] args) {
List<Integer> iList = new ArrayList<Integer>();
List<String> sList = new ArrayList<String>();
new Iterate().method1(1, iList);
new Iterate().method1(1, sList);
}
}
let's say I have this simple MyArray class, with two simple methods: add, delete and an iterator. In the main method we can see how it is supposed to be used:
public class MyArray {
int start;
int end;
int[] arr;
myIterator it;
public MyArray(){
this.start=0;
this.end=0;
this.arr=new int[500];
it=new myIterator();
}
public void add(int el){
this.arr[this.end]=el;
this.end++;
}
public void delete(){
this.arr[this.start]=0;
this.start++;
}
public static void main(String[] args){
MyArray m=new MyArray();
m.add(3);
m.add(299);
m.add(19);
m.add(27);
while(m.it.hasNext()){
System.out.println(m.it.next());
}
}
And then MyIterator should be implemented somehow:
import java.util.Iterator;
public class myIterator implements Iterator{
#Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
#Override
public Object next() {
// TODO Auto-generated method stub
return null;
}
#Override
public void remove() {
// TODO Auto-generated method stub
}
}
MyIterator should iterate arr from MyArray class, from start to end values; both are also attributes of MyArray. So, as MyIterator should use MyArray attributes, how should MyIterator be implemented? Perhaps I can send the current object in the initialization:
it=new myIterator(this);
But I guess it's not the best soultion. Or maybe MyArray itself should implement Iterator interface? How is this solved?
EDIT:
Ok, thanks to everybody. This was a simple example of what I wnat to do, so don't care about fixed length array. Waht I really want to do is a circular FIFO, that's why start and end are the cursors.
This circular FIFO will be an array of pairs of ints with, e.g., size 300: int[][] arr=new int[300][2].
When iterating a circular array I have to take care if the counter arrives to the end and make it start from the beginning, so this is how I have solved it:
if (this.start >= this.end ) temp_end=this.end+this.buff.length;
else temp_end=this.end;
int ii;
int j=0;
int[] value=new int[2];
for(int i=this.start; i<temp_end; i++){
ii=i% this.arr.length;
value=this.buff[ii];
//do anything with value
}
But I would like to avoid worrying about these things and just iterate in a simple way, I can do this with iterator interface, but then I have 2 problems: the first one I already explained and has been solved by many answers, and the second one is that my array is made of pairs of ints, and I can't use iterator with primitive types.
Its very unusual to maintain an iterator as an instance variable of the class. You can only traverse the array once - probably not what you want. More likely, you want your class to provide an iterator to anyone that wants to traverse your array. A more traditional iterator is below.
Java 5+ code - I haven't tried to compile or run, so it may be contain errors (not near a dev machine right now). It also uses autobox'ing for converting Integer to int.
public class MyArray implements Iterable<Integer> {
public static class MyIterator implements Iterator<Integer> {
private final MyArray myArray;
private int current;
MyIterator(MyArray myArray) {
this.myArray = myArray;
this.current = myArray.start;
}
#Override
public boolean hasNext() {
return current < myArray.end;
}
#Override
public Integer next() {
if (! hasNext()) throw new NoSuchElementException();
return myArray.arr[current++];
}
#Override
public void remove() {
// Choose exception or implementation:
throw new OperationNotSupportedException();
// or
//// if (! hasNext()) throw new NoSuchElementException();
//// if (currrent + 1 < myArray.end) {
//// System.arraycopy(myArray.arr, current+1, myArray.arr, current, myArray.end - current-1);
//// }
//// myArray.end--;
}
}
....
// Most of the rest of MyArray is the same except adding a new iterator method ....
public Iterator<Integer> iterator() {
return new MyIterator();
}
// The rest of MyArray is the same ....
}
Also note: be careful of not hitting that 500 element limit on your static array. Consider using the ArrayList class instead if you can.
In my opinion it is better to implement MyArray as common Iterable object, so it can be used in a for statement.
My suggestion:
/**
* My array
*/
public class MyArray<TItem> implements Iterable<TItem>
{
/**
* Internal used iterator.
*/
private class MyArrayIterator<TItem> implements Iterator<TItem>
{
private MyArray<TItem> _array;
/**
* #param array The underlying array.
*/
public MyArrayIterator(MyArray<TItem> array)
{
this._array = array;
}
/**
* Gets the underlying array.
*
* #return The underlying array.
*/
public MyArray<TItem> getArray() {
return this._array;
}
#Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
#Override
public TItem next() {
// TODO Auto-generated method stub
return null;
}
#Override
public void remove() {
// TODO Auto-generated method stub
}
}
public void add(int el){
// do add
}
public void delete(){
// do delete
}
#Override
public Iterator<TItem> iterator() {
// TODO Auto-generated method stub
return new MyArrayIterator<TItem>(this);
}
}
As I said you can use it in a for statement:
private static void test(MyArray<String> strArray)
{
for (String str: strArray) {
// do something
}
}
Iterator is an interface . Iterator<E> which means only Object can go here (E) .
Iterator<Integer> is legal but Integer<int> is not because int is primitive data type
You can change the array to the ArrayList and then iterate over this arraylist. I added getIterator() method that returns the arraylist.iterator() and test it in main() method
import java.util.ArrayList;
import java.util.Iterator;
public class MyArray {
int start;
int end;
ArrayList<Integer> arr;
public MyArray() {
this.start = 0;
this.end = 0;
arr = new ArrayList<Integer>(500);
}
public void add(int el) {
arr.add(el);
this.end++;
}
public void delete() {
arr.remove(arr.size()-1);
this.start++;
}
public Iterator<Integer> getIterator(){
return arr.iterator();
}
public static void main(String[] args) {
MyArray m = new MyArray();
m.add(3);
m.add(299);
m.add(19);
m.add(27);
Iterator<Integer> it = m.getIterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
My suggestion is to let MyArray implement the interface java.lang.Iterable and create an instance of an iterator per iterator() call (as an anonymous class). Then you can use an instance of MyArray directly in a foreach construct:
public class MyArray implements Iterable {
// ...
// Only arr is needed now as an instance variable.
// int start;
// int end;
int[] arr;
// myIterator it;
/**
* From interface Iterable.
*/
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
// The next array position to return
int pos = 0;
public boolean hasNext() {
return pos < arr.length;
}
public Integer next() {
if(hasNext())
return arr[pos++];
else
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
}
Update: According to BertF's comment I updated my code to make it clear, that the only instance variable for class MyArray is now arr. The state for the iterator is now inside the anonymous Iterator implementation. So you can create multiple iterator instances which don't interfere each other.
EDIT: this does not work for arrays of primitive types:
you could use Arrays for this:
it = new Arrays.asList(arr).subList(start, end).iterator();
END OF EDIT
If you really want to implement your own iterator, I would suggest an internal class in this scenario. This way you can access MyArray.this from myIterator.
public class MyArray {
....
private class myIterator implements Iterator{
....
}
}
MyArray should implement the Iterator as it is also responsible for maintaining the array. Simple encapsulation principle.