Big O runtime - indexOf LinkedList/ ArrayList - arraylist

I have a question considering Big O runtime and the indexOf method within LinkedList, and ArrayList. How can I come up with a Big O runtime assumption and how would it be different in a Linked List as opposed to an Array List?
LinkedList indexOf()
public int indexOf(Object value)
{
int results = -1;
boolean done = false;
Node<E> ref = head.next;
for(int i = 0; i < size && !done; i++)
{
if(value.equals(ref.value))
{
results = i;
done = true;
}
ref = ref.next;
}
return results;
}
ArrayList indexOf()
if (o == null) {
for (int i = 0; i < size; i++)
if (Values[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(Values[i]))
return i;
}
return -1;
I apologize if this is a trivial question to some but I am going to need to understand how to come up with a Big O runtime of a method.

In both these implementations you have nothing better to do than go over the list one element at a time and compare it to the value you're looking for. At the worst case, you'd be going through the entire list, giving a complexity of O(n).

Related

How to add JSpinner output into array and check for duplicate

I'm working on a code which has a JSpinner, which gives an output and moves it to int = n, I want to save every JSpinner output and add it into an array to check for duplicates and once found the JPanel should close itself or say you lost.
Scanner s = new Scanner(System.in);
int n = (Integer) spinner.getValue();
if (isPrime(n)) {
Input.setText(n + " is a prime number");
score++;
Highscore.setText("Score: " + score);
int[] array = new int[4];
for(int i=0; i<4;i++)
{
array[i]= (Integer) spinner.getValue();
}
for (int i=0; i < 4;i++)
{
System.out.println(array[i]);
}
Spinner output into array but it fills the whole array with one number example: [3,3,3,3].
private <T> boolean duplicate(T... array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = i + 1; j < array.length; j++)
{
if (array[i] != null && array[i].equals(array[j])) {
return true;
dispose();
}
}
}
return false;
}
Duplicate check
I tried adding the user input from the JSpinner into an array and from there to check for duplicates.
Searched Online but could'nt find anything.
This if my first post so if you need anything more you can tell me.

Make both sorting algorithms put the values in descending order. Then create drive class to test the two algorithms

I am extremely confused on how to reverse these sorting methods. Any help would be appreciated.
I have looked up and tried researching but I could not find anything to do with this type of comparable list.
public class Sorting
{
public static void selectionSort(Comparable[] list)
{
int min;
Comparable temp;
for (int index = 0; index < list.length-1; index++)
{
min = index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}
public static void insertionSort(Comparable[] list)
{
for (int index = 1; index < list.length; index++)
{
Comparable key = list[index];
int position = index;
while (position > 0 && key.compareTo(list[position-1]) < 0)
{
list[position] = list[position-1];
position--;
}
list[position] = key;
}
}
}
I think if you change:
if (list[scan].compareTo(list[min]) < 0)
to
if (list[scan].compareTo(list[min]) > 0)
it will sort in reverse order.
Here is the api
int compareTo(T o)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

ArrayList Method Returns a null ArrayList, main program cannot access

I'm trying to create a basic function that calls on a method that creates the 2D ArrayList that will be used further in the main program to do things like calculate the row and column sums as well as print out the triangle.
However, after it runs the ArrayList returns null. What's going on?
Thanks,
public class Trib
{
private ArrayList<ArrayList<Integer>> triangle;
private int Asize;
public Trib (int size)
{
// convert the argument to type 'int' to be used in the program
Asize = size;
// create an ArrayList of ArrayLists, it will have 'size' number ArrayLists contained within
ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>(size);
// create the inner ArrayLists
for (int i = 0; i < size; i++)
{
// add to index 'i' of our ArrayList a new ArrayList of size (i+1)
triangle.add(new ArrayList<Integer>(i+1));
for (int j = 0; j <= i; j++)
{
if (j==0 || j == i)
{
triangle.get(i).add(1);
}
else
triangle.get(i).add(triangle.get(i-1).get(j-1)+triangle.get(i-1).get(j));
System.out.print(triangle.get(i).get(j) + " ");
}
System.out.println();
}
triangle.clone();
}
public void printTriangle()
{
System.out.print(triangle.get(1).get(1));
/*for (int i = 0; i < Asize; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(triangle.get(1).get(1) + " ");
}
System.out.println();
}*/
}
/*public Trib()
{
this(5);
}*/
/*public int Psize()
{
return triangle.size();
}
public ArrayList<Integer> sumRows()
{
ArrayList<Integer> row_sum = new ArrayList<Integer>(Asize);
for (int i = 0; i < Asize; i++)
{
for (int j = 0; j < i; j++)
{
row_sum.add(triangle.get(i).get(j));
}
}
return row_sum;
}
public ArrayList<Integer> sumCols()
{
ArrayList<Integer> col_sum = new ArrayList<Integer>(Asize);
for (int i = 0; i < Asize; i++)
{
for (int j = 0; j < i; j++)
{
col_sum.add(triangle.get(i).get(i));
}
}
return col_sum;
}*/
public static void main(String[] args)
{
if(args.length < 1)
{
System.err.println("Sorry, this program needs an integer argument.");
System.exit(1);
}
Trib pt = new Trib(Integer.parseInt(args[0]));
pt.printTriangle();
//ArrayList<Object> sum_rows = new ArrayList<Object>(pt.Psize());
// sum_rows.add;
System.out.println("\nHere are the sum of rows:");
//for (int i = 0; i < pt.Psize(); i++)
//System.out.println(sum_rows.get(i));
//ArrayList<Integer> sum_cols = new ArrayList<Integer>(pt.Psize());
System.out.println("\nHere are the sum of columns:");
//for (int i = 0; i < pt.Psize(); i++)
//System.out.printf("%-5d", sum_cols.get(i));
}
}
Watch out what's what you are doing: Notice that you have TWO variables named "triangle": The first one is an instance variable and the second is a local variable, which is the only one you have initialized.
My suggestion to avoid this common mistake is to pre-pend "this." to any use of what you intend must be an instance variable. And, if in doubt, if you use a development environment as Eclipse, press CTRL and click on your variable to navigate to the point where it is declared.

iterate sum over array java

I need to perform an accumulative sum in an array in Java according to iteration number, like this.
for (int it = 1; it < 3; it++) {
for(int i = 0; i < simSource.length; i++) {
for (int j = 0; j < resulting.length; j++) {
results [i][j] = resulting[i][j] + (simSource[i] * 0.5)/3;
}
}
newResults = results;
}
Thus, in each iteration, the values in the array results[i][j] increase, and these values, are stored in an array. This process is repeated until the max amount of iterations - in the first "for".
My question/trouble is: How to store in the array the final values after each iteration?
Thank you very much.

Checking to see what elements in 2 arraylists are the same

I'm working on some code to build a wingbox from some the geometry of a wing. I have an integer arraylist (node) of all the nodes on the wing, and I have an integer arralist (ohnode) of the nodes that I want to trim in order to make my wingbox. How can I compare the 2 arraylists and delete the elements in the node arraylist so that all I'm left with are the nodes that weren't in the ohnode arraylist?
I haven't found many good examples comparing arraylists, most of them were for arrays. Would code for arrays work for arraylists, like arrays.equal?
Basically, I want to run through through all the nodes on the node arraylist, and if
Display common elements from two arraylists
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
a1.add("a");
a1.add("b");
a1.add("c");
a2.add("a");
a2.add("b");
a2.add("c");
a2.add("d");
a2.add("e");
int array1Size = a1.size();
int array2Size = a2.size();
if (a1.size() > a2.size()) {
int k = 0;
for (int i = 0; i < a2.size(); i++) {
if (((String)a1.get(i)).equals((String)a2.get(i))) {
System.out.println((String)a2.get(i));
}
k = i;
}
}
else {
int k = 0;
for (int i = 0; i < a1.size(); i++) {
if (((String)a1.get(i)).equals((String)a2.get(i))) {
System.out.println((String)a1.get(i));
}
k = i;
}
}
then loop through all common elements in 2nd arraylist, and delete it.