In: Computer Science
Hi
I have a java code for my assignment and I have problem with one of my methods(slice).the error is Exception in thread "main" java.lang.StackOverflowError
Slice method spec:
Method Name: slice
Return Type: Tuple (with proper generics)
Method Parameters: Start (inclusive) and stop (exclusive) indexes.
Both of these parameters are "Integer" types
(not "int" types). Like "get" above, indexes may
be positive or negative. Indexes may be null.
Description:
Given this Tuple:
[10, 20, 30, 40]
Example slice results
Start | Stop | Result (New Tuple) |
---|---|---|
0 | 1 | [10] |
0 | 2 | [10, 20] |
0 | 3 | [10, 20, 30] |
0 | 4 | [10, 20, 30, 40] |
null | 1 | [10] |
null | 3 | [10, 20, 30] |
-2 | -1 | [30] |
-2 | null | [30, 40] |
-1 | null | [40] |
null | null | [10, 20, 30, 40] |
Note: Each result is a new tuple
Example usage:
tuple2 = tuple1.slice(0, 1);
tuple2 = tuple1.slice(-1, null);
My code is the following:
import java.util.*;
public class Tuple1 <T>{
private List<T> elements;
public Tuple1(List<T> newElements){
this.elements=newElements;
}
@SafeVarargs
public Tuple1(T...newElements){
List<T> elementsss=new
ArrayList<T>();
for(T i:newElements){
elementsss.add(i);
}
this.elements=elementsss;
}
public Tuple1(Tuple1<T> tuple){
elements=tuple.elements;
}
public List<T> getElements(){
return
this.elements;
}
public T get(Integer index){
if(index<0){
return
this.getElements().get(size()+index);
}
else{
return
this.getElements().get(index);
}
}
public int size(){
return getElements().size();
}
public T getFirst(){
return this.get(0);
}
public T getLast(){
return this.get(-1);
}
public List toList(){
List<T> copy=new
ArrayList<>(this.getElements());
return
copy;
}
public Integer confirmStart(Integer start){
Integer
star=0;
if
(start==null){
star=0;
}
else if
(start<0){
star=this.size()+start;
}
else {
star=start;
}
return
star;
}
public Integer confirmStop(Integer stop){
Integer
sto=0;
if(stop==null){
sto=this.size();
}
else
if(stop<0){
sto=this.size()+stop-1;
}
else{
sto=stop-1;
}
return
sto;
}
public Tuple1 <T> slice(Integer start,Integer
stop){
List<T>
list= new ArrayList<T>();
Integer
star=this.confirmStart(start);
Integer
sto=this.confirmStop(stop);
list=this.getElements().subList(star,sto);
Tuple1 <T>
t;
t= new Tuple1
<T>(list);
return t;
}
@Override
public String toString(){
String output="Tuple Elementst: "+"
"+this.getElements()+
"\n Get Element:
"+this.get(0)+
"\nSize:
"+this.size()+
"\nFirst
Element:"+" "+this.getFirst()+
"\nLast Element
:"+" "+this.getLast()+
"\nconfirmStart:
"+" "+this.confirmStart(1)+
"\nConfirm
Stop:"+" "+this.confirmStop(-1)+
"\ntoList:"+"
"+this.toList()+
"\nSlice:"+"
"+this.slice(0,2);
return
output;
}
public static void main (String[]args){
List<String>elementss=new
ArrayList<String>();
elementss.add("Haitham");
elementss.add("Lindsey");
elementss.add("Lamar");
elementss.add("Narmin");
Tuple1 <String> elm;
elm=new
Tuple1<String>(elementss);
System.out.println(elm.toString());
System.out.println("================================");
Tuple1 <Integer> elms;
elms=new
Tuple1<Integer>(1,2,3,4,5);
System.out.println(elms.toString());
}
}
I fixed the code. It was getting into infinite loop because of the toString() method. Inside toString(), the slice(0,2) is being repeated called. Ideally toString() should only give a representation of the object. In this case only the elements need to be listed and not all the functions being called. So I have cleaned the code a bit and changed the main to show the proper usage. Hope it helps. For 2nd example with numbers, I have shown how to use negative index and null to show elements being fetched from last.
import java.util.*;
public class Tuple1 <T>{
private List<T> elements;
public Tuple1(List<T> newElements){
this.elements=newElements;
}
@SafeVarargs
public Tuple1(T...newElements){
List<T> elementsss=new
ArrayList<T>();
for(T i:newElements){
elementsss.add(i);
}
this.elements=elementsss;
}
public Tuple1(Tuple1<T> tuple){
elements=tuple.elements;
}
public List<T> getElements(){
return this.elements;
}
public T get(Integer index){
if(index<0){
return
this.getElements().get(size()+index);
}
else{
return
this.getElements().get(index);
}
}
public int size(){
return getElements().size();
}
public T getFirst(){
return this.get(0);
}
public T getLast(){
return this.get(-1);
}
public List toList(){
List<T> copy=new ArrayList<>(this.getElements());
return copy;
}
public Integer confirmStart(Integer start){
Integer star=0;
if (start==null){
star=0;
}
else if (start<0){
star=this.size()+start;
}
else {
star=start;
}
return star;
}
public Integer confirmStop(Integer stop){
Integer sto=0;
if(stop==null){
sto=this.size();
}
else if(stop<0){
sto=this.size()+stop-1;
}
else{
sto=stop;
}
return sto;
}
public Tuple1 <T> slice(Integer start,Integer stop){
List<T> list= new ArrayList<T>();
Integer
star=this.confirmStart(start);
Integer
sto=this.confirmStop(stop);
list=this.getElements().subList(star,sto);
Tuple1 <T> t;
t= new Tuple1
<T>(list);
return t;
}
@Override
public String toString(){
return elements.toString();
}
public static void main (String[]args){
List<String>elementss=new
ArrayList<String>();
elementss.add("Haitham");
elementss.add("Lindsey");
elementss.add("Lamar");
elementss.add("Narmin");
Tuple1 <String> elm;
elm=new
Tuple1<String>(elementss);
String output="Tuple Elementst: "+"
"+elm+
"\n Get Element: "+elm.get(0)+
"\nSize: "+elm.size()+
"\nFirst Element:"+" "+elm.getFirst()+
"\nLast Element :"+" "+elm.getLast()+
"\nconfirmStart: "+"
"+elm.confirmStart(1)+
"\nConfirm Stop:"+" "+elm.confirmStop(-1)+
"\ntoList:"+" "+elm.toList()+
"\nSlice:"+" "+elm.slice(0,2);
System.out.println(output);
System.out.println("================================");
Tuple1 <Integer> elms;
elms=new
Tuple1<Integer>(1,2,3,4,5);
output="Tuple Elementst: "+"
"+elms+
"\n Get Element:
"+elms.get(0)+
"\nSize: "+elms.size()+
"\nFirst Element:"+"
"+elms.getFirst()+
"\nLast Element :"+"
"+elms.getLast()+
"\nconfirmStart: "+"
"+elms.confirmStart(1)+
"\nConfirm Stop:"+"
"+elms.confirmStop(-1)+
"\ntoList:"+"
"+elms.toList()+
"\nSlice:"+"
"+elms.slice(-2,null);
System.out.println(output);
}
}
output
----
Tuple Elementst: [Haitham, Lindsey, Lamar, Narmin]
Get Element: Haitham
Size: 4
First Element: Haitham
Last Element : Narmin
confirmStart: 1
Confirm Stop: 2
toList: [Haitham, Lindsey, Lamar, Narmin]
Slice: [Haitham, Lindsey]
================================
Tuple Elementst: [1, 2, 3, 4, 5]
Get Element: 1
Size: 5
First Element: 1
Last Element : 5
confirmStart: 1
Confirm Stop: 3
toList: [1, 2, 3, 4, 5]
Slice: [4, 5]