+ 1
How can I insert an integer data to my Linkedlist at specified index? Sample code below
9 Answers
+ 2
For adding element at specific position
add(int index, element)
if there's not sufficient element in the list
U will get an error message
'IndexOutOfBound'
import java. util.LinkedList ;
public class Program {
public static void main(String[] args) {
LinkedList<Integer> table = new LinkedList<Integer>();
table.add(5);
// table. add(6);
// table. add(7);
// table. add(8);
table. add(2,9);
System.out.println(table);
}
}
Check this code for better understanding
+ 1
Azalea
try this
import java. util.LinkedList ;
public class Program {
public static void main(String[] args) {
LinkedList<Integer> table = new LinkedList<Integer>();
table.add(5);
table.add(6);
System.out.println(table);
}
}
+ 1
As I understand it, linked lists doesn't use indices in its operation. One just have to iterate through the nodes from one end to the other, while there is still a valid next node to walk through.
You can still walk through the nodes, counting the number of nodes as index, and update the node's data as necessary (when found), or abort the operation when there is no node found at specified index.
0
Programmer how can I place the input 5 at index 2?
0
Azalea
Hope u will find what u want https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/LinkedList.html
0
if I do, table[2] = data;
it says cannot convert int to Linkedlist. how could i fix this?
0
Martin Taylor Programmer
what if I am only allowed to use this test class? https://code.sololearn.com/ci3CpQ5UCSm9/?ref=app
How could I input the data to the specified index?