Sumityadav

Sumityadav

How to get the first and last occurrence of the specified elements in a linked list in Java?

Hello all, I am new to learning Java Programming and want to learn java from scratch. I was writing a Java program to get the first and last occurrence of the specified elements in a linked list. Here is the Code:

import java.util.LinkedList;
import java.util.Iterator;
  public class program {
  public static void main(String[] args) {
    // create an empty linked list
     LinkedList<String> l_list = new LinkedList<String>();
   // use add() method to add values in the linked list
          list.add("Red");
          ist.add("Green");
          list.add("Black");
          list.add("Pink");
          list.add("orange");
      
      // print original list
   System.out.println("Original linked list:" + list);  
 
   // Find first element of the List
    Object first_element = list.getFirst();
    System.out.println("First Element is: "+first_element);
 
    // Find last element of the List
    Object last_element = list.getLast();
    System.out.println("Last Element is: "+ element);
 }
}

When I was trying to run the code I shows and error, I do not understand how to solve this. can any one please help in in this?

Marked As Solved

faust

faust

There are a few typing errors in your code, for example " ist.add(“Green”);" instead of “list.add(“Green”);”

And System.out.println("Last Element is: "+ element); should be System.out.println("Last Element is: "+ last_element);

with these changes your code shall run correctly.

Besides, it’s important to note that in Java we use cammelCase instead of snake_case, you should use firstElement and lastElement instead of first_element and last_element, this won’t affect the code but is the common pattern used by the vast majority of the Java Developers, so it’s better to follow it too.

Popular Backend topics Top

AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
GermaVinsmoke
Reading Programming Elixir 1.6 book, I’ve completed part 1 of the book. Now I’m thinking of reading Elixir in Action. What do you all sug...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
Fl4m3Ph03n1x
Background I am trying to get a Github Action working with Windows and Bakeware because I am trying to create a release using it. Howeve...
New
Fl4m3Ph03n1x
Background I am trying to encode a structure into json format using the Jason library. However, this is not working as expected. Code L...
New
Fl4m3Ph03n1x
Background PS: the following situation describes an hypothetical scenario, where I own a company that sells things to customers. I have ...
New
sona11
In Java, if I try to do.equals() on a null string, a null pointer error is issued. I’m wondering whether I can perform the following if I...
New
Fl4m3Ph03n1x
Background I am a fan of dialyzer and friends (looking at Gradient) and I try to have sepcs in my code as much as I can. To this end, I a...
New
Fl4m3Ph03n1x
Background I have a module that uses TypedStruct to create structs. This is the code: defmodule Shared.Data.Authorization do @moduledo...
New
New

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New