LinkedList z użyciem pętli for - dodawanie elementu na koniec listy

1

import java.util.;
import java.lang.
;
import java.io.*;
class Book {
private String author;
private String title;
public Book(String author, String title) {
this.author = author;
this.title = title;
}
public String toString() {
return "Title: "" + title + "", author: " + author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean equals(Object o){
Book e = (Book) o;
return (author.equals(e.getAuthor())) &&
(title.equals(e.getTitle()));
}
}
class LinkedListBooksLibrary {
private LinkedList<Book> theList;
private Book book;
public LinkedListBooksLibrary(LinkedList<Book> newList) {
this.theList = newList;
this.book = book;
}
public void addBooksToLinkedList() {
for(int n = 0; n < 100000; n++) {
theList.add(new Book("Author nr " + n, "Title nr " + n));
}
System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
System.out.println();
}
public void linkedListProcessor() {
theList.addLast(new Book("Author nr " + n, "Title nr " + n));
}
}
class LinkedListExample
{
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Book> theList = new LinkedList<Book>();
LinkedListBooksLibrary process = new LinkedListBooksLibrary(theList);
process.addBooksToLinkedList();
process.linkedListProcessor();
}
}

chciałbym dodać ostatni element do listy
nie chce sie kompilować
błąd : cannot find symbol symbol: variable n location: class LinkedListBooksLibrary, line 41
cannot find symbol symbol: variable n location: class LinkedListBooksLibrary, line 41
prosszę o pomoc - brak już pomysłów - dziekuję

0

Po pierwsze, sformatuj kod, żeby się go dało czytać.
Po drugie, korzystasz z nieistniejącej zmiennej n,

0
import java.util.;
import java.lang.;
import java.io.*;
class Book {
private String author;
private String title;
public Book(String author, String title) {
this.author = author;
this.title = title;
}
public String toString() {
return "Title: \"" + title + "\", author: " + author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean equals(Object o){
Book e = (Book) o;
return (author.equals(e.getAuthor())) &&
(title.equals(e.getTitle()));
}
}
class LinkedListBooksLibrary {
private LinkedList<book> theList;
private Book book;
public LinkedListBooksLibrary(LinkedList<book> newList) {
this.theList = newList;
this.book = book;
}
public void addBooksToLinkedList() {
for(int n = 0; n < 100000; n++) {
theList.add(new Book("Author nr " + n, "Title nr " + n));
}
System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
System.out.println();
}
public void linkedListProcessor() {
theList.addLast(new Book("Author nr " + n, "Title nr " + n));
}
}
class LinkedListExample
{
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<book> theList = new LinkedList<book>();
LinkedListBooksLibrary process = new LinkedListBooksLibrary(theList);
process.addBooksToLinkedList();
process.linkedListProcessor();
}
}

0
bogdans napisał(a):

Po pierwsze, sformatuj kod, żeby się go dało czytać.
Po drugie, korzystasz z nieistniejącej zmiennej n,

ok, ale jak mam zapisać abym mógł dodać do listy element na końcu?
:(

0

Wstaw kod w znaczniki dopasowane do języka (lista rozwijana na prawym końcu paska narzędzi).

public void addBooksToLinkedList() {
    for(int n = 0; n < 100000; n++) {
        theList.add(new Book("Author nr " + n, "Title nr " + n));
    }
//zmienna n zadeklarowana w wyrażeniu for przestaje istnieć po zakończeniu pętli.
    System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
    System.out.println();
}
public void linkedListProcessor() {
    theList.addLast(new Book("Author nr " + n, "Title nr " + n));
}
0
public void addBooksToLinkedList() {
    for(int n = 0; n < 100000; n++) {
        theList.add(new Book("Author nr " + n, "Title nr " + n));
    }
//zmienna n zadeklarowana w wyrażeniu for przestaje istnieć po zakończeniu pętli.
    System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
    System.out.println();
}
public void linkedListProcessor() {
    theList.addLast(new Book("Author nr " + n, "Title nr " + n));
}
0

Przecież metoda add(element) zawsze dodaje na końcu.
Może chodzi Ci o coś takiego:

int booksNumber = 10000;
public void addBooksToLinkedList() {
    for(int n = 0; n < booksNumber; n++) {
        theList.add(new Book("Author nr " + n, "Title nr " + n));
    }
public void linkedListProcessor() {
    theList.addLast(new Book("Author nr " + booksNumber, "Title nr " + booksNumber));
}

P.S. O wcięciach w kodzie słyszałeś?

0
import java.util.;
import java.lang.;
import java.io.*;
class Book {
private String author;
private String title;
public Book(String author, String title) {
this.author = author;
this.title = title;
}
public String toString() {
return "Title: \"" + title + "\", author: " + author;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean equals(Object o){
Book e = (Book) o;
return (author.equals(e.getAuthor())) &&
(title.equals(e.getTitle()));
}
}
class LinkedListBooksLibrary {
private LinkedList<book> theList;
private Book book;
public LinkedListBooksLibrary(LinkedList<book> newList) {
this.theList = newList;
this.book = book;
}
public void addBooksToLinkedList() {
for(int n = 0; n < 100000; n++) {
theList.add(new Book("Author nr " + n, "Title nr " + n));
}
System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
System.out.println();
}
public void linkedListProcessor() {
theList.addLast(new Book("Author nr " + n, "Title nr " + n));
}
}
class LinkedListExample
{
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<book> theList = new LinkedList<book>();
LinkedListBooksLibrary process = new LinkedListBooksLibrary(theList);
process.addBooksToLinkedList();
process.linkedListProcessor();
}
}
0
bogdans napisał(a):

Przecież metoda add(element) zawsze dodaje na końcu.
Może chodzi Ci o coś takiego:

int booksNumber = 10000;
public void addBooksToLinkedList() {
    for(int n = 0; n < booksNumber; n++) {
        theList.add(new Book("Author nr " + n, "Title nr " + n));
    }
public void linkedListProcessor() {
    theList.addLast(new Book("Author nr " + booksNumber, "Title nr " + booksNumber));
}

P.S. O wcięciach w kodzie słyszałeś?

kod działa. dziękuję serdecznie za pomoc.
córke mam rekach jak pisze teraz - juz wciecia poprawiam.
jeszcze raz dziekuje

0

Poprawiłem kod i masz dwie metody które dodają na koniec element do listy:

import java.util.*;
import java.lang.*;
import java.io.*;

class Book {
	
private String author;
private String title;

public Book(String author, String title) {
this.author = author;
this.title = title;
}

public String toString() {
return "Title: \"" + title + "\", author: " + author;
}

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}

public boolean equals(Object o){
Book e = (Book) o;
return (author.equals(e.getAuthor())) &&
(title.equals(e.getTitle()));
}

}

class LinkedListBooksLibrary {
	
private LinkedList<Book> theList;

public LinkedListBooksLibrary(LinkedList<Book> newList) {
this.theList = newList;

}

public void addBooksToLinkedList() {
for(int n = 0; n < 100000; n++) {
theList.add(new Book("Author nr " + n, "Title nr " + n));
}
System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
System.out.println();
}

public void linkedListProcessorInsertNext() {
	
int n = theList.size();
theList.addLast(new Book("Author nr " + n, "Title nr " + n));

System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
System.out.println();
}

public void linkedListProcessorInsertLast(int n) {
	
if( n > theList.size())
{
	theList.addLast(new Book("Author nr " + n, "Title nr " + n));

	System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
	System.out.println();
}

}

}

class LinkedListExample
{
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Book> theList = new LinkedList<Book>();
LinkedListBooksLibrary process = new LinkedListBooksLibrary(theList);
process.addBooksToLinkedList();
process.linkedListProcessorInsertNext();
process.linkedListProcessorInsertLast(100002);
}
}
0
gk1982 napisał(a):

Poprawiłem kod i masz dwie metody które dodają na koniec element do listy:

import java.util.*;
import java.lang.*;
import java.io.*;

class Book {
	
private String author;
private String title;

public Book(String author, String title) {
this.author = author;
this.title = title;
}

public String toString() {
return "Title: \"" + title + "\", author: " + author;
}

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}

public boolean equals(Object o){
Book e = (Book) o;
return (author.equals(e.getAuthor())) &&
(title.equals(e.getTitle()));
}

}

class LinkedListBooksLibrary {
	
private LinkedList<Book> theList;

public LinkedListBooksLibrary(LinkedList<Book> newList) {
this.theList = newList;

}

public void addBooksToLinkedList() {
for(int n = 0; n < 100000; n++) {
theList.add(new Book("Author nr " + n, "Title nr " + n));
}
System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
System.out.println();
}

public void linkedListProcessorInsertNext() {
	
int n = theList.size();
theList.addLast(new Book("Author nr " + n, "Title nr " + n));

System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
System.out.println();
}

public void linkedListProcessorInsertLast(int n) {
	
if( n > theList.size())
{
	theList.addLast(new Book("Author nr " + n, "Title nr " + n));

	System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
	System.out.println();
}

}

}

class LinkedListExample
{
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Book> theList = new LinkedList<Book>();
LinkedListBooksLibrary process = new LinkedListBooksLibrary(theList);
process.addBooksToLinkedList();
process.linkedListProcessorInsertNext();
process.linkedListProcessorInsertLast(100002);
}
}

Dziekuje. Bardzo fajny zapis <thumb up="up">

0
trebiano napisał(a):
gk1982 napisał(a):

Poprawiłem kod i masz dwie metody które dodają na koniec element do listy:

import java.util.*;
import java.lang.*;
import java.io.*;

class Book {
	
private String author;
private String title;

public Book(String author, String title) {
this.author = author;
this.title = title;
}

public String toString() {
return "Title: \"" + title + "\", author: " + author;
}

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}

public boolean equals(Object o){
Book e = (Book) o;
return (author.equals(e.getAuthor())) &&
(title.equals(e.getTitle()));
}

}

class LinkedListBooksLibrary {
	
private LinkedList<Book> theList;

public LinkedListBooksLibrary(LinkedList<Book> newList) {
this.theList = newList;

}

public void addBooksToLinkedList() {
for(int n = 0; n < 100000; n++) {
theList.add(new Book("Author nr " + n, "Title nr " + n));
}
System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
System.out.println();
}

public void linkedListProcessorInsertNext() {
	
int n = theList.size();
theList.addLast(new Book("Author nr " + n, "Title nr " + n));

System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
System.out.println();
}

public void linkedListProcessorInsertLast(int n) {
	
if( n > theList.size())
{
	theList.addLast(new Book("Author nr " + n, "Title nr " + n));

	System.out.println("Quantity of elements in the LinkedList the collection: " + theList.size());
	System.out.println();
}

}

}

class LinkedListExample
{
public static void main (String[] args) throws java.lang.Exception
{
LinkedList<Book> theList = new LinkedList<Book>();
LinkedListBooksLibrary process = new LinkedListBooksLibrary(theList);
process.addBooksToLinkedList();
process.linkedListProcessorInsertNext();
process.linkedListProcessorInsertLast(100002);
}
}

Dziekuje. Bardzo fajny zapis <thumb up="up">

a jak z tego LinkedLista chciałbym stworzyć teraz HashMap z kilkoma milionami obiektów, dodając obiekty po pętli for?

0
HashMap<String, String> newMap = new HashMap<String, String>();
				for(Map.Entry<String, String> entry :newMap.entrySet())
					for(int n = 0; n < 100; n++){
				newMap.put(????????????????) }
0
trebiano napisał(a):
HashMap<String, String> newMap = new HashMap<String, String>();
				for(Map.Entry<String, String> entry :newMap.entrySet())
					for(int n = 0; n < 100; n++){
				newMap.put(????????????????) }

Pętla nie zakręci się ani razu bo chcesz iterować po pustej mapie, poza tym pętla foreach w zupełności nie jest Ci potrzebna.

Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 100; ++i)
    map.put("key" + i, "value" + i);
0
jarzi napisał(a):
trebiano napisał(a):
HashMap<String, String> newMap = new HashMap<String, String>();
				for(Map.Entry<String, String> entry :newMap.entrySet())
					for(int n = 0; n < 100; n++){
				newMap.put(????????????????) }

Pętla nie zakręci się ani razu bo chcesz iterować po pustej mapie, poza tym pętla foreach w zupełności nie jest Ci potrzebna.

Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < 100; ++i)
    map.put("key" + i, "value" + i);

Dziekuje zmieniłem i działa

1 użytkowników online, w tym zalogowanych: 0, gości: 1